Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

comparing chars with pointer to cha

Status
Not open for further replies.

papous

MIS
Feb 2, 2003
3
US
comparing chars with pointer to char
the code below is suppose to read from a file and to print out all alphanumeric characters(a-z,A-Z,0-0) spaces and newlines, throwing away the rest.


Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef char * STRNG;

STRNG code[80];

STRNG get_line(FILE * inf) {
    STRNG s = NULL;
    int count = 0;
    int tok;            // must be int - EOF is an int, not a char

    while ( (tok=getc(inf)) != EOF && tok != '\n' ) {
        if ( s == NULL ) s = malloc( 80 );
        s[count++] = tok;
    }
    if ( tok != EOF ) {
        if ( s == NULL ) s = malloc( 80 );  // possibly a newline on its own
        s[count++] = tok;                   // append the \n
        s[count] = '\0';
    }
    return s;
}

int main(int argc, char *argv[]) {
    char    *filename;
	char    *loc;
    FILE    *stream;
    int     i,j, n = 0;            // Moved C++ declaration
    char  alpha[64]={'q','w','e','r','t','y','u','i','o','p'
,'a','s','d','f','g','h','j','k','l','m','n',
 	'b','v','c','x','z','Q','W','E','R','T','Y','U','
 I','O','P','L','K','J','H','G','F','D','S','A','Z'
,
 	'X','C','V','B','N','M','1','2','3','4','5','6','
7','8','9','0',' ','\n'};

    filename =argv[1];
    stream=fopen(filename,&quot;r&quot;);
    if(stream ==NULL)
    {
        printf(&quot;Enter a valid parameter.Program will exit.\n&quot;);
        exit(1);
    }

    while ( (code[n]=get_line(stream)) != NULL ) {
        n++;
    }

    printf( &quot;%d lines found\n&quot;, n );
    for ( i = 0 ; i < n ; i++ )
	    {
         for(j=0;j<64;j++)
			 {
			 loc=strstr(code[i],alpha[j]);
			 if (loc!=NULL){
				 printf(&quot;%s&quot;,alpha[j]);
				           }
			 }

         }

    return 0;  // 0 is success
}

anybody can suggest something better or help solve it?
 
I think this simple code will does the same thing (get character [A-Z|a-z|0-9|' '|'\n'].

#include <stdio.h>
#include <conio.h>

FILE *fin; // filename for input file.
int ch; // variable to hold a character being read.


/**
* This program uses command line argument. It needs only
* one argument that is, the input file.
*/

int main(int argc, char **argv)
{
clrscr();
if (argc < 2) {
puts(&quot;usage: program_file input_file&quot;);
puts(&quot;Try again.&quot;);
return 1;
}

if ( (fin = fopen(argv[1], &quot;r&quot;)) == NULL ) {
printf(&quot;Error while opening file: %s \n&quot;, argv[1]);
return 1;
}

while ( (ch = getc(fin)) != EOF )
{
if ((ch>='A' && ch<='Z')||(ch>='a' && ch<='z') ||
(ch>='0' && ch<='9')||(ch==' ')||(ch=='\n'))
putchar(ch);
}

return 0;
}

/** Thank you **/
 
Hi, in ur way...

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef char * STRNG;
STRNG code[80];
STRNG get_line(FILE * inf)
{
STRNG s = NULL;
int count = 0;
int tok; // must be int - EOF is an int, not a char

s = malloc( 80 ); // add code for error checking
while ( (tok=getc(inf)) != EOF && tok != '\n' && count < 80 ) //otherwise memory overwrite may occur
{
if( (tok >= 48 && tok <= 57) || (tok >= 65 && tok <= 90) || (tok >= 97 && tok <= 122)) // ascii values of 0 -9, A -Z and a - z
s[count++] = tok;
}
return s;
}

int main(int argc, char *argv[]) {
char *filename;
char *loc;
FILE *stream;
int i,j, n = 0; // Moved C++ declaration

// check the number of command line argument
filename =argv[1];
stream=fopen(filename,&quot;r&quot;);
if(stream ==NULL)
{
printf(&quot;Enter a valid parameter.Program will exit.\n&quot;);
exit(1);
}

while ( (code[n]=get_line(stream)) != EOF ) {
n++;
}
fclose(stream);
printf( &quot;%d lines found\n&quot;, n );
for ( i = 0 ; i < n ; i++ )
{
printf(&quot;%s&quot;,code);
free(code) // the dynamically allocated memory shold be deallocated.
}
return 0; // 0 is success
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top