comparing chars with pointer to char

This is a discussion on comparing chars with pointer to char within the C Programming forums, part of the General Programming Boards category; 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, ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    13

    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,"r");
        if(stream ==NULL)
        {
            printf("Enter a valid parameter.Program will exit.\n");
            exit(1);
        }
    
        while ( (code[n]=get_line(stream)) != NULL ) {
            n++;
        }
    
        printf( "%d lines found\n", n );
        for ( i = 0 ; i < n ; i++ )
    	    {
             for(j=0;j<64;j++)
    			 {
    			 loc=strstr(code[i],alpha[j]);
    			 if (loc!=NULL){
    				 printf("%s",alpha[j]);
    				           }
    			 }
    
             }
    
        return 0;  // 0 is success
    }
    anybody can suggest something better or help solve it?

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    13
    i changed the code into this.now the characters that will be printed are passed through command line via argv, but i still do something wrong.help guys
    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,k,m=0, n = 0;
        char   *alpha;
    	alpha=argv[1];
        filename =argv[2];
        stream=fopen(filename,"r");
    
        if(stream ==NULL)
        {
            printf("Enter a valid parameter.Program will exit.\n");
            exit(1);
        }
    
        while ( (code[n]=get_line(stream)) != NULL ) {
            n++;
        }
    while((k=getc(alpha))!='\0')
    	{
    		m++;
    	}
    	printf("%d\n",m);
        for ( i = 0 ; i < n ; i++ )
    	    {
             for(j=0;j<m;j++)
    			 {
    			 loc=strchr(code[i],alpha[j]);
    			 if (loc!=NULL){
    				 printf("%s",alpha[j]);
    				           }
    			 }
    
             }
    
        return 0;  // 0 is success
    }

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    164
    I suggest you use isalpha() instead of searching through an array. You can also use a loopup table.
    // Gliptic

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    13

    help

    somebody help guys

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Don't be so impatient. We're not servents to respond to your every beck and call.

    You've already been told what to do. Use a loop and the function 'isalpha'.

    For example, given a buffer 'buf' that is a string of characters, to check them all, you can simply do:

    for( x = 0; buf[x]; x++ )
    if( isalpha( buf[x] ) ) printf("buf[%d] is %c\n", x, buf[x] );

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,758
    > while((k=getc(alpha))!='\0')
    This doesn't even compile - or you're ignoring a serious warning. alpha is a char*, and getc expects a FILE*

    Besides, it looks like you should be doing
    m = strlen( alpha );

    > printf("%s",alpha[j]);
    To print a single character, its
    printf("%c",alpha[j]);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 06-16-2006, 10:23 PM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 12:58 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21