fork download
  1. #include <stdio.h>
  2.  
  3. int find(char *string, char *substring) {
  4. int i, j;
  5.  
  6. for (i = 0; string[i] != '\0'; i++) {
  7. for (j = 0; substring[j] != '\0' && string[i + j] == substring[j]; j++);
  8. if (substring[j] == '\0') {
  9. return i; // substring found at index i
  10. }
  11. }
  12.  
  13. return -1; // substring not found
  14. }
  15.  
  16. int main() {
  17. char string[] = "Hello, World!";
  18. char substring[] = "World";
  19. int index = find(string, substring);
  20. if (index != -1) {
  21. printf("Substring found at index: %d\n", index);
  22. } else {
  23. printf("Substring not found\n");
  24. }
  25. return 0;
  26. }
Success #stdin #stdout 0s 5304KB
stdin
Standard input is empty
stdout
Substring found at index: 7