deneme bonusu veren sitelerbahis casinomakrobetceltabetpinbahispolobetpolobet girişpinbahis girişmakrobet girişpulibet girişmobilbahis girişkolaybet giriş C Program Replace All Occurrences Of A Character With Another In String
Latest :

C Program Replace All Occurrences Of A Character With Another In String

C program to replace all occurrences of a character with another in a string – In this article, we will discuss the multiple ways to replace all occurrences 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 ways described in this piece are as follows:

  • Using Standard Model
  • 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 we can see with this example, a string is to be entered first up.

The string entered here is “hello world”

We need to specify which character needs to be replaced with what.

The character ‘o’ has been replaced here with ‘0’.

So, after replacing, the string became:

“hell0 w0rld”

Thus, the methods used to do so in C programming are as follows:

Using Standard Method

  1. Read the entered string using gets(s) function and store the string into the variable ‘s’.

2) Read the character which we want to replace all its occurrences in the string and store in the variable c1.

3) Read the character by which we replace all occurrences of the element, store in the variable c2.

4) The for loop iterates through the string until the last character of the string becomes to null.

replace each occurrence of the character c1 with c2.

5) Print the string after replacing all occurrences of the character with another character in the string.

Output:

Using Function

  1. The main() function calls the replacechar(char *s, char c1, char c2) function to replace all occurrences of the character with another character.

2) Replace c1 with c2 for each occurrence of the character c1 in the string using the for loop which iterates through the string until the end of the string with the structure for(i=0;s[i];i++).

3) Print the string after replacing all occurrences of the character with another character.

Output:

Using Recursion

  1. The main() function calls the stringlength(char *s) function which returns the length of the string.

2) The main() function calls the recursive function replacechar(char *s, char c1, char c2) to replace all occurrences of the character with another character.

3) a) i=0.If the character at s[i] is null then exit from the function.

b) If it is not null then compare s[i] with the character c1. If it matches then replace s[i] with c2.

Compare s[i] with the character c1.If it matches then replace s[i] with c2.

Increase the i value to search for the next occurrence and call the function itself.

4) The function calls itself until the last character of the string.

5) The main() function prints the string after replacing all occurrences of the character with another character.

Output:
deneme bonusu veren sitelerbahis casinomakrobetceltabetpinbahispolobetpolobet girişpinbahis girişmakrobet girişpulibet girişmobilbahis girişkolaybet 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 ...