C program to replace the first occurrence of a character in a string; Through this tutorial, we will learn how to replace first occurrence of a character in a string using for loop, while loop, recursion and functions in c programs.
Programs and Algorithm To Replace First Occurrence of a Character in a String in C
- Algorithm To Replace First Occurrence of a Character in a String
- C Program To Replace First Occurrence of a Character in a String using For Loop
- C Program To Replace First Occurrence of a Character in a String using While Loop
- C Program To Replace First Occurrence of a Character in a String using Function
- C Program To Replace First Occurrence of a Character in a String using Recursion
Algorithm To Replace First Occurrence of a Character in a String
Use the following algorithm to write a program to replace first occurrence of a character with in a string; as follows:
- Input string from user, store it in some variable.
- Input character to replace and character new character from user, store it in some variables.
- Iterate a loop from start of string str to end.
- Inside the loop, swap old character with new character and terminate the loop.
- Print result.
C Program To Replace First Occurrence of a Character in a String using For Loop
#include <stdio.h> #include <string.h> int main() { char s[1000],c1,c2; int i; printf("Enter the string : "); gets(s); printf("Enter a character replace: "); c1=getchar(); getchar(); printf("\nEnter character to replace %c with: ",c1); c2=getchar(); printf("\n before replace:%s",s); for(i=0;s[i];i++) { if(s[i]==c1) { s[i]=c2; break; } } printf("\nafter replace:%s",s); return 0; }
The output of the above c program; as follows:
Enter the string : hello Enter a character replace: h Enter character to replace h with: w before replace:hello after replace:wello
C Program To Replace First Occurrence of a Character in a String using While Loop
/* C Program to Replace First Occurrence of a Character in a String */ #include <stdio.h> #include <string.h> int main() { char str[100], ch, Newch; int i = 0; printf("\n Please Enter any String : "); gets(str); printf("Enter a character replace: "); scanf("%c", &ch); getchar(); printf("\nEnter character to replace %c with: ",ch); scanf("%c", &Newch); while(i <= strlen(str)) { if(str[i] == ch) { str[i] = Newch; break; } i++; } printf("\n The Final String after Replacing First occurrence of '%c' with '%c' = %s ", ch, Newch, str); return 0; }
The output of the above c program; as follows:
Please Enter any String : hello Enter a character replace: h Enter character to replace h with: w The Final String after Replacing First occurrence of 'h' with 'w' = wello
C Program To Replace First Occurrence of a Character in a String using Function
/* C Program to Replace First Occurrence of a Character in a String */ #include <stdio.h> #include <string.h> void Replace_FirstOccurrence(char *str, char ch, char Newch); int main() { char str[100], ch, Newch; int i = 0; printf("\n Please Enter any String : "); gets(str); printf("\n Please Enter the Character that you want to Replace : "); scanf("%c", &ch); getchar(); printf("\n Please Enter the New Character : "); scanf("%c", &Newch); Replace_FirstOccurrence(str, ch, Newch); printf("\n The Final String after Replacing First occurrence of '%c' with '%c' = %s ", ch, Newch, str); return 0; } void Replace_FirstOccurrence(char *str, char ch, char Newch) { int i; for(i = 0; str[i] != '\0'; i++) { if(str[i] == ch) { str[i] = Newch; break; } } }
The output of the above c program; as follows:
Please Enter any String : fans Please Enter the Character that you want to Replace : f Please Enter the New Character : v The Final String after Replacing First occurrence of 'f' with 'v' = vans
C Program To Replace First Occurrence of a Character in a String using Recursion
#include <stdio.h> #include <string.h> void replacechar(char *s,char c1,char c2) { static int i=0; if(s[i]==c1) { s[i]=c2; return; } i++; replacechar(s,c1,c2); } int main() { char s[1000],c1,c2; printf("Enter the string : "); gets(s); printf("Enter a character replace: "); c1=getchar(); getchar(); printf("\nEnter character to replace %c with: ",c1); c2=getchar(); printf("\n before replace:%s",s); replacechar(s,c1,c2); printf("\nafter replace:%s",s); return 0; }
The output of the above c program; as follows:
Enter the string : sony Enter a character replace: s Enter character to replace s with: w before replace:sony after replace:wony