C Books Guide and List
C++ Books Guide and List
Best Java Books

3,749 questions

4,049 answers

114 comments

10,705 users

Write a C program to find occurrence of a second string in the first string?

0 votes
2,580 views
asked Jan 7, 2014 by sara Guru (24,570 points)

1 Answer

0 votes
#include<stdio.h>
 
int main(){
    char str[40];
    int i, j = 0;
    scanf("%s", str);
    for(i = 1; str[i] != '\0';i++){
        if(str[i-1] == str[i]){//For consecutive repeated characters
            j = i-1;        //If found, we place an index j there
            j++;
            while(1){           //This loops checks for mutiple
                if(str[j] == str[j+1])  //occurrences of a character
                    j++;
                else
                    break;
            }
            printf("Character %c is repeated %d times\n", str[i], j - (i-1)+1);
            i = j;
        }
    }
    if(j == 0){
        printf("No repeated character");
    }
    return 0;
}
answered Jul 18, 2014 by anonymous

Related questions

0 votes
1 answer 200 views
0 votes
1 answer 1,439 views
0 votes
1 answer 50 views
...