Skip to content

Commit

Permalink
Create 4. string check if it has a sub string.c
Browse files Browse the repository at this point in the history
  • Loading branch information
shad-ct authored Oct 15, 2024
1 parent ada59ee commit 27ce0cf
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions LabRecord/4. string check if it has a sub string.c
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");
}
}

0 comments on commit 27ce0cf

Please sign in to comment.