casinojojobetpulibet girişOnwin Güncel Giriştürkçe altyazılı pornocanlı bahis casinocanlı bahis casino siteleriOnwin Güncel Girişholiganbetholiganbet girişholiganbet güncel giriş C Program To Replace Last Occurrence Of A Character In String | C Programs
Latest :

C Program To Replace Last Occurrence Of A Character In String | C Programs

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

  1. 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.

Output:

Using Function

  1. 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.

Output:

Using Recursion

  1. 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.

Output:
casinojojobetpulibet girişOnwin Güncel Giriştürkçe altyazılı pornocanlı bahis casinocanlı bahis casino siteleriOnwin Güncel Girişholiganbetholiganbet girişholiganbet güncel giriş
x

Check Also

C Program To Print Number Of Days In A Month | Java Tutoring

C program to input the month number and print the number of days in that ...