fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #define MAX_SIZE 100 // Maximum string size
  4.  
  5. int main()
  6. {
  7. char str[MAX_SIZE], word[MAX_SIZE];
  8. int i, index, found = 0;
  9.  
  10. /* Input string and word from user */
  11. printf("Enter any string: ");
  12. gets(str);
  13. printf("Enter word to be searched: ");
  14. gets(word);
  15.  
  16.  
  17. /* Run loop from start to end of string */
  18. index = 0;
  19. while(str[index] != '\0')
  20. {
  21.  
  22. /* If first character of word matches with the given string */
  23. if(str[index] == word[0])
  24. {
  25. /* Match entire word with current found index */
  26. i=0;
  27. found = 1;
  28. while(word[i] != '\0')
  29. {
  30. if(str[index + i] != word[i])
  31. {
  32. found = 0;
  33. break;
  34. }
  35.  
  36. i++;
  37. }
  38. }
  39.  
  40. /* If the word is found then get out of loop */
  41. if(found == 1)
  42. {
  43. break;
  44. }
  45.  
  46. index++;
  47. }
  48.  
  49. /* Print success message if the word is found */
  50. if(found == 1)
  51. {
  52. printf("\n'%s' is found at index %d.", word, index);
  53. }
  54. else
  55. {
  56. printf("\n'%s' is not found.", word);
  57. }
  58.  
  59. return 0;
  60. }
Success #stdin #stdout 0s 4544KB
stdin
tracn
tr
stdout
Enter any string: Enter word to be searched: 
'tr' is found at index 0.