C program to replace the last occurrence of a character with another in a string – In this article, we will describe the multiple means to replace the last occurrence of a character with another in a string in C programming.
Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly. The compiler has also been added with which you can execute it yourself.
The means used in this particular piece are as follows:
- Using Standard Method
- Using Function
- Using Recursion
A string is nothing but an array of characters. The value of a string is determined by the terminating character. Its value is considered to be 0.
As you can see with the image uploaded, you need to enter a string accordingly.
The string entered here is as follows:
“welcome to cbeginners”
Then we need to mention the character which we need to replace with.
Here, the character ‘o’ is replaced with ‘t’.
So, the final string is as follows:
“welcome tt cbeginners”
Thus, the various methods mentioned in this piece in C programming are as follows:
Using Standard Method
- Read the entered string and store the string into the character array variable ‘s’ using gets(s) function.
2) Read the character which we want to replace its last occurrence in the string and store in the variable c1.
3) Read the character which we want to replace with, and store the character in the variable c2.
4) The 1st for loop iterates through the string until end of the character. The last index of the string indicates the length of the string.
5) Decrease the length value by 1.The for loop iterates through the string in reverse order
The for loop iterates through the string in reverse order with the structure for(i=length of the string-1; i>0; i–).
If the element of the string s[length-1] match with the entered character then replace s[i] with c2 and terminate the “for loop”. If it does not match then repeat the loop again by decreasing i value.
6) Print the string after replacing the last occurrence of the character.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#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++); i--; for(;i>=0;i--) { if(s[i]==c1) { s[i]=c2; break; } } printf("\nafter replace:%s",s); return 0; } |
Output:
1 2 3 4 5 6 7 |
Enter the string: welcome to cbeginners Enter a character replace: o Enter character to replace with o : t before replace:welcome to cbeginners after replace:welcome tt cbeginners |
Using Function
- The main() function calls the stringlength(char *s) function to get the length of the string.
2) The main() call the replacechar() function to replace the last occurrence of the character with another character in the string.
a) For loop iterates through the string in reverse order with the structure for(i=length of the string-1; i>0; i–)
If the element of the string is equal to the entered string then replace the element with the character and terminate the loop. If it doesn’t match, iterate the loop with i–.
3) Print the string after replacing the last occurrence of the element.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
#include <stdio.h> #include <string.h> int stringlength(char *s) { int j; for(j=0;s[j];j++); return j; } void replacechar(char *s,char c1,char c2) { int i=stringlength(s)-1; for(;i>=0;i--) { if(s[i]==c1) { s[i]=c2; break; } } } 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; } |
Output:
1 2 3 4 5 6 7 |
Enter the string: welcome to cbeginners Enter a character replace: o Enter character to replace o with : t before replace:welcome to cbeginners after replace:welcome tt cbeginners |
Using Recursion
- The stringlength(char *s) returns the length of the string.
2) The main() function calls the recursive function replacechar() to replace the last occurrence of the element.
i=length of the string-1.
a) if the element s[i] match with the character c1 then replace s[i] with c2 and return back to the main() function.
b) if it doesn’t match then decrease the i value by 1 and call the function itself to find the last occurrence of the element.
3) The function calls itself until it finds the last occurrence of the element in the string.
4) Print the string after replacing the last occurrence of the element in the string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
#include <stdio.h> #include <string.h> int stringlength(char *s) { int j; for(j=0;s[j];j++); return j; } void replacechar(char *s,char c1,char c2) { static int i=stringlength(s)-1; 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; } |
1 2 3 4 5 6 7 |
Enter the string : welcome to cbeginners Enter a character replace: o Enter character to replace o with : t before replace:welcome to cbeginners after replace:welcome tt cbeginners |