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:
- /*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
- 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.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAX_STR 100
- int main(void)
- {
- //Variable declarations
- char document[MAX_STR], target[MAX_STR], replacement[MAX_STR];
- //Welcome user
- 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");
- 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");
- printf("NULL string is entered for the string to be searched, ie. only the <Enter Key>\nis pressed.\n");
- for (;;)
- {
- //Prompt for and scan in user input
- printf("\nPlease enter a string to search: ");
- fgets(document, MAX_STR, stdin);
- //Terminates loop when conditions met
- if ((strlen(document) == 0) || (strlen(document) == 1))
- return 0;
- else
- document[strlen(document) - 1] = '\0';
- //Continue scanning user input
- printf("\nPlease enter the target string to search for: ");
- scanf("%s", &target);
- printf("\nPlease enter the target replacement string: ");
- scanf("%s", &replacement);
- //Get string lengths
- int docL, tarL, repL;
- docL = strlen(document);
- tarL = strlen(target);
- repL = strlen(replacement);
- //Pointer to 1st occurence of target in document
- char *occurence = strstr(document, target);
- //Checks to see if target string occurs in document string
- if (occurence == NULL)
- {
- printf("\nNo match found!\n");
- printf("%s\n", document);
- }
- else //Replace occurence of target in document
- {
- strncpy(occurence, target, tarL);
- puts(document);
- printf("%s\n", document);
- }
- }
- }
Learn More :
Perform
Search
- Dictionary Word Search C Program
- C Depth First Search
- C Program To Search A Number Inside The Array
- C Program To Write Data In A File And Search Data From File
- C Program to search an element using linear search or binary search (menu driven program)
- C Program Recursive function that searches for a given file in a given folder
- Pre Order, Post order, In order Implement Binary Tree using linked list
- BST Tree in C
- C Program Search Function For Sequence
- Linked List For Getting Employee Details, Display and Search For Salary C Program
- Menu driven program in the creation,display,search, insertion and deletion of a node in the linked list
String
- Write a c program to check given string is palindrome number or not.
- C Program to Convert Text String to Binary
- C Program String - Alphabet Count Example
- C Program String Example
- C Program String Count Example
- C Program String Example
- C Program to Print a String Without Use Semicolon.
- C Program To Find Length Of A String Including Blank Spaces, Tabs, And Other Special Characters
- C Program To Find Length Of String And Concatenate Two Strings
- C Program To Find The Length, Reverse Of A String And To Check It Is Palindrome or Not
- C Program To Print String In Formatted Output Form
- C Program To Reverse The String Using Pointer
- C Program Concatenating Two Strings Into A Third System
- C Program Date from string DDDD-DD-DD
- BUILDS A STRING FROM THE LSB BITS OF EACH PIXEL
- C Program Finds Sub-String Search in String
- C Program to concatenate two strings without using string functions
- C Program to Count Vowels, Consonants and Spaces in a string
- C Program to convert a string to upper case
- C Program Anagrams
- C Program to find string length without library function
- C Program to Check Given String is Palindrome or Not
- C Program to Converts a Number to a String.
- Design a C function that shortens a string to 8 characters
Target
replace