Write an algorithm that reads a string s and a character c, prints on the screen the number of times c appears in s and the first position of c in s.


Difficulty level
Video recording
This exercise is mostly suitable for students
#include <stdio.h>
#define size 100

int main()
{
    char s1[size], c;  
    int i, j, nb;         

    printf("Enter the string s: ");
    gets(s1);
    
    printf("Enter the character c: ");
    c=getchar();

    nb=0;
    i=0;
    while(s1[i]) 
    {
        
        if(s1[i]==c)
        {
            printf("c found at position %d\n",i);
            nb++;
        }

        i++;
    }
    
    printf("c appears in s2 %d times\n", nb);
    return 0;
}

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Automata