C Program Performs a search and replace for a specified target and replacement string

How to write a C Program performs a search and replace for a specified target and replacement string. It will prompt for a string to be searched, a target string to be replaced, and then prompt for a replacement string to replace each target occurrence. The program will repeat, asking for three new strings until a NULL string is entered for the string to be searched, ie. the <Enter Key> only being pressed in C Programming Language ?


Solution:

  1. /*Description: This program performs a search and replace for a specified target and replacement string. It will prompt for a string to be searched, a target string
  2.                          to be replaced, and then prompt for a replacement string to replace each target occurrence. The program will repeat, asking for three new strings until
  3.                          a NULL string is entered for the string to be searched, ie. the <Enter Key> only being pressed.
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9.  
  10. #define MAX_STR 100
  11.  
  12.  
  13. int main(void)
  14. {
  15.         //Variable declarations
  16.         char document[MAX_STR], target[MAX_STR], replacement[MAX_STR];
  17.  
  18.         //Welcome user
  19.         printf("This program performs a search and replace for a specified target and\nreplacement string. It will prompt for a string to be searched, a target string\n");
  20.         printf("to be replaced, and then prompt for a replacement string to replace each target\noccurrence. The program will repeat, asking for three new strings until a\n");
  21.         printf("NULL string is entered for the string to be searched, ie. only the <Enter Key>\nis pressed.\n");
  22.  
  23.         for (;;)
  24.         {
  25.                 //Prompt for and scan in user input
  26.                 printf("\nPlease enter a string to search: ");
  27.                 fgets(document, MAX_STR, stdin);
  28.  
  29.                 //Terminates loop when conditions met
  30.                 if ((strlen(document) == 0) || (strlen(document) == 1))
  31.                         return 0;
  32.                 else
  33.                         document[strlen(document) - 1] = '\0';
  34.  
  35.                 //Continue scanning user input
  36.                 printf("\nPlease enter the target string to search for: ");
  37.                 scanf("%s", &target);
  38.                 printf("\nPlease enter the target replacement string: ");
  39.                 scanf("%s", &replacement);
  40.  
  41.                 //Get string lengths
  42.                 int docL, tarL, repL;
  43.                 docL = strlen(document);
  44.                 tarL = strlen(target);
  45.                 repL = strlen(replacement);
  46.  
  47.                 //Pointer to 1st occurence of target in document
  48.                 char *occurence = strstr(document, target);
  49.                
  50.                 //Checks to see if target string occurs in document string
  51.                 if (occurence == NULL)
  52.                 {
  53.                         printf("\nNo match found!\n");
  54.                         printf("%s\n", document);
  55.                 }
  56.                 else //Replace occurence of target in document
  57.                 {
  58.                         strncpy(occurence, target, tarL);
  59.                         puts(document);
  60.                         printf("%s\n", document);
  61.                 }
  62.         }
  63. }


Learn More :