-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 4. string check if it has a sub string.c
- Loading branch information
Showing
1 changed file
with
27 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
|
||
int main() { | ||
char str[100], substr[100]; | ||
|
||
printf("Enter your main string : "); | ||
scanf("%s",str); | ||
printf("Enter your sub string : "); | ||
scanf("%s",substr); | ||
|
||
int len = strlen(str), sublen = strlen(substr), flag = 0, sup =0; | ||
|
||
for(int i = 0;i<=len-sublen;i++){ | ||
if (strncmp(str + i, substr, sublen) == 0) { // strncmp(str1, str2, times to check) | ||
flag = 1; | ||
sup = i; | ||
} | ||
} | ||
if(flag ==1){ | ||
printf("Sub string found at %d",sup); | ||
} | ||
else{ | ||
printf("Sub string not found"); | ||
} | ||
} |