Problem Description
Write a C program to search all occurrences of a word in given string using loop
Array Index starts from 0CODING ARENA
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100
int main()
{
char str[MAX_SIZE];
char word[MAX_SIZE];
int i, j, found;
int strLen, wordLen;
scanf("%[^\n]%*c",str);
scanf("%s",word);
strLen = strlen(str);
wordLen = strlen(word);
for(i=0; i<strLen; i++)
{
found = 1;
for(j=0; j<wordLen; j++)
{
if(str[i + j] != word[j])
{
found = 0;
break;
}
}
if(found == 1)
{
printf("%d\n",i);
}
}
return 0;
}
Test Case 1
Input (stdin)srm university srm university srm
Expected Output0 15
Test Case 2
Input (stdin)srm srm srm srm srm srm
Expected Output0 4 8 12 16
No comments:
Post a Comment