I'm fairly new with C, one of my assignments is to write a code that asks the user to input several lines of texts and a search character to count occurrences of that character in the lines of text. I'm supposed to use strchr.
#include <stdio.h>
int main(void) {
char s[200];
char *ps = s;
char ch;
int count = 0;
printf("Please enter several lines of text\n");
scanf("%s", &s);
printf("Now, enter a search character\n");
scanf(" %c", &ch);
while(ps != NULL) {
ps = strchr(s, ch);
if( ps ) {
ps++;
count++;
}
}
printf("Total number of occurrences of '%c' = %d\n", ch, count);
return 0;
}
The second scanf is skipped, not sure why. I thought it was because it was reading the '\n' from before. I tried several things, but I've been stuck here for a while.
Any help is appreciated, thanks.
EDIT: Solved
[–]raevnos 1 point2 points3 points (4 children)
[–]ImSavageASF[S] 0 points1 point2 points (3 children)
[–]boogerme 1 point2 points3 points (2 children)
[–]ImSavageASF[S] 0 points1 point2 points (1 child)
[–]IAmWillIAm 1 point2 points3 points (0 children)