![]() |
| | #1 |
| Registered User Join Date: Jan 2002
Posts: 1,020
| character occurrence program not working thnx in advance Code: /* character occurence count */
#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 );
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;
}
|
| Nutshell is offline | |
| | #2 |
| B26354 Join Date: Jan 2002
Posts: 631
| Hey Nutshell, The user is prompted to enter a number ("Number of lines to be inputted:") and scanf() puts the value into the variable numLines, but doesn't pull the carriage-return off the buffer (stdin). Then when you reach gets() ("Enter string:"), the carriage return causes gets() to grab an empty line. Consider using fgets() to get data from the user.
__________________ Jason Deckard |
| Deckard is offline | |
| | #3 |
| Guest
Posts: n/a
| i haven't learnt fgets() yet. Anyway to fix the problem using my original code? thnx |
|
| | #4 |
| Registered User Join Date: Jan 2002
Posts: 1,020
| the above reply was mine, forgot to log in... |
| Nutshell is offline | |
| | #5 |
| B26354 Join Date: Jan 2002
Posts: 631
| Yeah, a cheap trick would be to put in a gets() right after the scanf(). This would pull the carriage-return off stdin. If you are allowed to fflush(), that would be the proper way to clean up stdin.
__________________ Jason Deckard |
| Deckard is offline | |
| | #6 |
| B26354 Join Date: Jan 2002
Posts: 631
| Re: character occurrence program not working Something like this, Nutshell: Code: printf( "Number of lines to be inputted: " );
scanf( "%d", &numLines );
/* Here is the new statement */
gets( tempStr );
for ( i = 0; i < numLines; i++ ) {
printf( "Enter string: " );
gets( tempStr );
strcat( string, tempStr );
}
__________________ Jason Deckard |
| Deckard is offline | |
| | #7 |
| Registered User Join Date: Jan 2002
Posts: 1,020
| thnx worked fine but if only a newline is needed can i just print '\n' after that scanf() ? |
| Nutshell is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Program not working | HAssan | C Programming | 5 | 01-11-2009 02:45 AM |
| Problem with my program i cant figure out... | youareafever | C Programming | 7 | 11-01-2008 11:56 PM |
| Printing "shapes" based on character lines and number of line inputs | matt_570 | C++ Programming | 11 | 10-07-2008 06:19 PM |
| Program Not working Right | raven420smoke | C++ Programming | 2 | 09-16-2005 03:21 AM |
| Simple Program not working right (in C) | DruzeTito | C Programming | 5 | 06-01-2002 10:14 PM |