Find the location of given sub string in a String
Get a String and Substring and find where the substring is present in String.
Sample Input 1:
entertainment ain
Sample Output 1:
6
Try your Solution
Strongly recommended to Solve it on your own, Don't directly go to the solution given below.
#include<stdio.h>
int main()
{
//write your code here
}
Program or Solution
#include<stdio.h>
#include<string.h>
int main()
{
char str[50],s[30];
int i,j,k,b;
fgets(str,50,stdin);
fgets(s,50,stdin);
for(i=0;i<=strlen(str)-strlen(s);i++)
{
j=0;
b=0;
if(str[i]==s[j])
{
for(k=i;j<=strlen(s)-1;k++,j++)
{
if(str[k]!=s[j])
{
b=1;
break;
}
}
if(b==0)
{
printf("Sub string is at %d",i);
return 0;
}
}
}
printf("sub String not found");
return 0;
}
Program Explanation
check whether first character of sub string is equal to any character in string.
If it is equal, then check remaining characters are equal to the characters in substring.
else move to next character.
Comments
Related Programs
- Print the characters of String with space
- Find the Length of given String
- Find the location of given Character in a String
- Count the occurences of given Character in a String
- Covert Lowercase characters to Uppercase Characters
- Covert Uppercase characters to Lowercase Characters
- Count the number of vowels in given string
- Compare two Strings are equal
- Concate two given Strings
- Count the occurences of given substring in a String
- Reverse the given String
- Check the given string is Palindronme
- Remove the spaces in given String
- Remove the Vowels in given String
coming Soon
coming Soon