Thread: help with xrater case

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    6

    help with xrater case

    Pls how can I make this to not differentiate between upper and lower cases?


    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
       int   occurrenceCount = 0;
       char   *searchPtr;
       char string[ 400 ];
       char tempStr[ 150 ];
       int numLines;
       int i;
       char key;
    
       printf( "Number of lines to be inputted: " );
       scanf( "%d", &numLines );
    
    	gets( tempStr );
    
       for ( i = 0; i < numLines; i++ ) {
          printf( "Enter string: " );
          gets( tempStr );
          strcat( string, tempStr );
       }
    
       printf( "Enter search character: " );
       scanf( "%c", &key );
    
       searchPtr = strchr( string, key );
    
       while ( searchPtr != NULL ) {
          ++occurrenceCount;
          searchPtr = strchr( searchPtr + 1, key );
       }
    
       printf( "\n\nTotal of occurrence character \'%c\' appeared in line(s) entered: %d\n\n",
               key, occurrenceCount );
    
       system( "PAUSE" );
       return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can explicitly program it that way, or you can use toupper() or tolower() to set an entire string to either upper or lower case, as you wish.

    An example of explicit code is:

    Code:
    char choice;
    
    scanf("%c", &choice);
    
    if(choice == 'Y' || choice == 'y')
      //do something, knowing that either case is handled for 
      // the variable named choice

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    6
    why cant I use scanf for to collect the input? when I try this it skips to the end of the program after asking for number of lines to be inputted.

    i.e.

    Code:
      printf( "Number of lines to be inputted: " );
       scanf( "%d", &numLines );
    
    	scanf("%[^\n]", tempStr );
    
       for ( i = 0; i < numLines; i++ ) {
          printf( "Enter string: " );
          scanf("%[^\n]", tempStr );
          strcat( string, tempStr );
       }

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The key thing to remember with scanf(), is that you have to be aware of the keyboard buffer, *AT* the time you the program arrives at the scanf() line of code.

    So after scanf() is used once, there will be one (at least) newline char \n, in the keyboard buffer.

    The best way to clean up the keyboard buffer, is to just pull all the char's off it. Normally, that should just be one newline char, so one

    getchar();

    will be enough. If you have multiple scanf()'s, you should have a getchar() after every one of them that is followed by a scanf() for a char or string.

    scanf()'s for numbers are OK, since, when scanf() is looking for a number, it will pull off newline char's, and keep on going, looking for a number.

    But for char's, it can't do that, because the newline char itself *IS* a char. So it has to take it and say "OK, I got what I came for", and exit.

    Making it seem as though scanf() has skipped over the whole line - but it didn't.

    So:

    Code:
    scanf("%d", myNumber);   //ok, it's the first scanf(), so no newline is in the keyboard buffer
    
    scanf("%d", myNumber);  //ok, scanf() jumps over the newline in the buffer
    
    scanf("%c", myChar); //not okay, put a getchar() before this line of code.
    If you have a [^] format command that works with your compiler's scanf() - fine, use it. If not, use getchar() as needed.

    Hope that helps.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-01-2009, 09:54 AM
  2. Segmentation fault!?!?!?
    By Viper187 in forum Windows Programming
    Replies: 57
    Last Post: 10-02-2008, 09:40 PM
  3. Format Precision & Scale
    By mattnewtoc in forum C Programming
    Replies: 1
    Last Post: 09-16-2008, 10:34 AM
  4. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM