#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int main(){
char *str = NULL;
char *sub = NULL;
size_t lent = 256;
if(getline(&str,&lent,stdin) == -1){
printf("no input for 1st line");
return 1;
}
if(getline(&sub,&lent,stdin) == -1){
printf("no input for 2nd line");
return 1;
}
int len = strlen(str);
int word_count = 0;
for(int i=0; i<=len-1; i++){
int j=0;
while(str[i+j]==sub[j] && sub[j]!='\0'){
j++;
}
if(sub[j]=='\0' && str[i+j] == ' '){
printf("%d found word : %d",i, word_count+1);
return 0;
}
if(str[i] == ' '){
word_count++;
}
}
printf("not found");
if(word_count == 0){
printf("string not found\n");
}
free(str);
free(sub);
}
#if 0
int is_word_boundary(char c) {
return isspace(c) || ispunct(c);
}
int main() {
char str[100];
char sub[20];
int i, j;
printf("Enter a string: ");
scanf("%[^\n]", str);
printf("Enter a substring: ");
scanf(" %[^\n]", sub);//vvimp takes 2nd line input
for (i = 0; i < strlen(str); i++) {
j = 0;
while (str[i+j] == sub[j] && sub[j] != '\0') {//imp
j++;
}
if(sub[j]=='\0' && (i == 0 || is_word_boundary(str[i-1])) && (str[i+j] == '\0' || is_word_boundary(str[i+j]))){
printf("Substring found at position %d\n", i);
return 0;//imp
}
}
printf("Substring not found\n");
return 0;
}
#endif
/*
here at i place it compares 1st letter so ex name .. n and then if it gets n then
it goes in j loop check if next elements like a is there after n and so one whole
name is there in 1st string then it returns true if not false and exit.
This version defines a helper function is_word_boundary that checks whether a
character is a word boundary (i.e. a space or punctuation mark). It then adds a
condition to the if statement that checks whether the characters before and after
the matched substring are word boundaries. This ensures that only complete words
are considered matches.
*/
To embed this program on your website, copy the following code and paste it into your website's HTML: