C program to replace the last occurrence of a character in a string; Through this tutorial, we will learn how to replace last occurrence of a character in a string using for loop, while loop and functions in c programs.
Programs and Algorithm To Replace Last Occurrence of a Character in a String in C
- Algorithm To Replace Last Occurrence of a Character in a String
- C Program To Replace Last Occurrence of a Character in a String using For Loop
- C Program To Replace Last Occurrence of a Character in a String using While Loop
- C Program To Replace Last Occurrence of a Character in a String using Function
Algorithm To Replace Last Occurrence of a Character in a String
Use the following algorithm to write a program to replace last 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 Last Occurrence of a Character in a String using For Loop
/* C Program to Replace Last Occurrence of a Character in a String */ #include <stdio.h> #include <string.h> int main() { char str[100], ch, Newch; int i, index; index = -1; 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); for(i = 0; str[i] != '\0'; i++) { if(str[i] == ch) { index = i; } } if(index != -1) { str[index] = Newch; } printf("\nafter replace:%s",str); return 0; }
The output of the above c program; as follows:
Please Enter any String : hate Enter a character replace: h Enter character to replace h with : v after replace:vate
C Program To Replace Last Occurrence of a Character in a String using While Loop
/* C Program to Replace Last Occurrence of a Character in a String */ #include <stdio.h> #include <string.h> int main() { char str[100], ch, Newch; int i, index; index = -1; 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) { index = i; } i++; } if(index != -1) { str[index] = Newch; } printf("\nafter replace:%s",str); return 0; }
The output of the above c program; as follows:
Please Enter any String : proramming Enter a character replace: r Enter character to replace r with : l after replace:prolamming
C Program To Replace Last Occurrence of a Character in a String using Function
/* C Program to Replace Last Occurrence of a Character in a String */ #include <stdio.h> #include <string.h> void Replace_LastOccurrence(char *str, char ch, char Newch); int main() { char str[100], ch, Newch; int i, index; index = -1; 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); Replace_LastOccurrence(str, ch, Newch); printf("\nafter replace:%s",str); return 0; } void Replace_LastOccurrence(char *str, char ch, char Newch) { int i, index; index = -1; for(i = 0; str[i] != '\0'; i++) { if(str[i] == ch) { index = i; } } if(index != -1) { str[index] = Newch; } }
The output of the above c program; as follows:
Please Enter any String : hello Enter a character replace: l Enter character to replace l with : r after replace:helro