C Tutorial/String/String Search


Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/includes/exception/MWExceptionHandler.php on line 179

Deprecated: Creation of dynamic property PPDStack::$accum is deprecated in /var/www/cppe.ru/www/includes/parser/Preprocessor_DOM.php on line 803

Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/includes/exception/MWExceptionHandler.php on line 179

Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/vendor/mediawiki/at-ease/src/Functions.php on line 48
Материал из C\C++ эксперт
Перейти к: навигация, поиск

Find occurrences of one string in another

#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void)
{
  char text[100];
  char substring[40];
  printf("\nEnter the string to be searched(less than 100 characters):\n");
  fgets(text, sizeof(text), stdin);
  printf("\nEnter the string sought (less than 40 characters ):\n");
  fgets(substring, sizeof(substring), stdin);
  /* overwrite the newline character in each string */
  text[strlen(text)-1] = "\0";
  substring[strlen(substring)-1] = "\0";
  int i;
  for(i = 0 ; (text[i] = toupper(text[i])) ; i++);
  for(i = 0 ; (substring[i] = toupper(substring[i])) ; i++);
   printf("\nThe second string %s found in the first.",((strstr(text, substring) == NULL) ? "was not" : "was"));
  return 0;
}
Enter the string to be searched(less than 100 characters):
     string
     
     Enter the string sought (less than 40 characters ):
     str
     
     The second string was found in the first.