searching for a character throughout 4 strings in c using strchr

246 views Asked by At

I'm having some trouble figuring out why when I run the program it won't let me scan the character I want to search for. It just jumps straight to the last printf statement.

int main()
{
char s1[100], s2[100], s3[100], s4[100];
char character
char *charSearch[3] = {s1, s2, s3, s4};
int counter = 0;
int i;

printf("Enter 4 lines of text: \n");
scanf("%s %s %s %s", &s1, &s2, &s3, &s4);
printf("Enter a any character to search:\n");

scanf("%c", &character);

for(i = 0; i < 3; i++)
{
while(charSearch[i] = strchr(  charSearch, character ))
{
counter++;
charsearch[i]++;
}
}
printf("Total occurrences of character %c is %d", character, counter);

return 0;
}
3

There are 3 answers

1
Ahmed Karaman On BEST ANSWER

First things first, you declared an array of char pointers of size 3 while giving it 4 elements : char *charSearch[3] = {s1, s2, s3, s4};

This should be : char *charSearch[4] = {s1, s2, s3, s4}; This declaration should also be done after scanning the contents of the 4 char array {s1,s2,s3,s4} and not before.


The name of any array is a pointer to its first element, thus when using scanf() with char arrays, you already pass the desired address by doing so : scanf("%s %s %s %s", s1, s2, s3, s4);

What you did here : scanf("%s %s %s %s", &s1, &s2, &s3, &s4); passed the address of the first address (sounds weird I know xD).


There is also that irritating bug with scanf("%c", &character); which if you've previously scanned anything ending with a new line, the character will take this \n as its input. To over come this, it should be written as : scanf(" %c", &character);


Now to strchr(), this functions takes two parameters, a char pointerto a string and the character to search for in the string and it returns another char pointer to the location of the character if it was found and NULL otherwise.

We'll need to do the following instead:

char *ptr = strchr( charSearch[i], character );
    while(ptr!=NULL)
    {
        ptr = strchr( ptr+1, character );
        counter++;
    }

What it does is that at the beginning of each iteration of the for loop, it declares a char pointer and initialize it to the return value of the strchr() function. Then we loop while this pointer is not equal to NULL and in each iteration increment our counter and our pointer to make it pointing to the next character in the string.


The final working version of your code should look like :

#include <stdio.h>
#include <string.h>
int main()
{
char s1[100], s2[100], s3[100], s4[100];
char character;
int counter = 0;
int i;

printf("Enter 4 lines of text: \n");
scanf("%s %s %s %s", s1, s2, s3, s4);
char *charSearch[4] = {s1, s2, s3, s4};
printf("Enter a any character to search:\n");

scanf(" %c", &character);

for(i = 0; i < 4; i++)
{
    char *ptr = strchr( charSearch[i], character );
    while(ptr!=NULL)
    {
        ptr = strchr( ptr+1, character );
    counter++;
    }
}
printf("Total occurrences of character %c is %d\n", character, counter);

return 0;
}
0
Fifi On

It's because when reading input using scanf, the input is read after the return key is pressed but the newline generated by the return key is not consumed by scanf, which means the next time you read a char from standard input there will be a newline ready to be read.

Source : Simple C scanf does not work?

0
Vlad from Moscow On

A Russian hacker will help you.:)

For starters according to the C Standard the function main without parameters shall be declared like

int main( void )

Try not to use magic numbers. The compiler shall issue a diagnostic message relative to this declaration

char *charSearch[3] = {s1, s2, s3, s4};

because there are more initializers than elements in the array.

This statement with scanf is unsafe and moreover the argument expressions are invalid.

scanf("%s %s %s %s", &s1, &s2, &s3, &s4);

It is desirable to place a blank in the format specifier in this statement

scanf("%c", &character);
      ^^^^

Otherwise blank characters (as for example the new ,line character) can be read.

The condition in the while statement does not make sense

while(charSearch[i] = strchr(  charSearch, character ))

The program can look the following way

#include <stdio.h>
#include <string.h>

#define SIZE 100

int main( void )
{
    char s1[SIZE], s2[SIZE], s3[SIZE], s4[SIZE];
    char * charSearch[] = { s1, s2, s3, s4 };
    const size_t N = sizeof(charSearch) / sizeof(*charSearch);
    char character;

    size_t counter = 0;

    printf( "Enter %zu lines of text: \n", N );

    for (size_t i = 0; i < N; i++)
    {
        charSearch[i][0] = '\0';
        fgets(charSearch[i], SIZE, stdin);
    }

    printf("Enter any character to search:\n");

    scanf(" %c", &character );

    for (size_t i = 0; i < N; i++)
    {
        for (char *p = charSearch[i]; ( p = strchr(p, character) ) != NULL; ++p )
        {
            ++counter;
        }
    }

    printf("Total occurrences of character %c is %zu\n", character, counter);

    return 0;
}

The program output might look like

Enter 4 lines of text:
Hello World
How are you?
Stackoverflow
I'm learning C
Enter any character to search:
e
Total occurrences of character e is 4