C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-04-2009, 12:13 PM   #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;
}
babyboi is offline   Reply With Quote
Old 11-04-2009, 12:20 PM   #2
Registered User
 
Join Date: Sep 2006
Posts: 2,517
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
Adak is offline   Reply With Quote
Old 11-04-2009, 01:44 PM   #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 );
   }
babyboi is offline   Reply With Quote
Old 11-05-2009, 02:15 AM   #4
Registered User
 
Join Date: Sep 2006
Posts: 2,517
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.
Adak is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Compiling Program as .exe with no dependancy on other files nitesh C++ Programming 2 11-01-2009 09:54 AM
Segmentation fault!?!?!? Viper187 Windows Programming 57 10-02-2008 09:40 PM
Format Precision & Scale mattnewtoc C Programming 1 09-16-2008 10:34 AM
Base converter libary cdonlan C++ Programming 22 05-15-2005 01:11 AM
Binary Search Trees Part III Prelude A Brief History of Cprogramming.com 16 10-02-2004 03:00 PM


All times are GMT -6. The time now is 03:47 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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