Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)
C Program Replace First Occurrence Of A Character With Another String
Latest :

C Program Replace First Occurrence Of A Character With Another String

C program to replace the first occurrence of a character with another in a string – In this article, we will detail in on the several means to replace the first 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 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 we can see in the photo uploaded above, we need to enter the string first up.

The string entered here is “hello world”.

Then, we have to select the first character to be replaced with another one.

The character ‘h’ here will be replaced with ‘H’.

Thus, the string after the replacement becomes “Hello world”.

Hence, the multiple ways to do the same in C programming are as follows:

Using Standard Method

  1. Read the user entered string using gets(s) function.

2) Read the character which we want to replace using getchar() and initialize the character to the variable c1.

3) Read the character to replace with using getchar() and store that character in the variable c2.

4) For loop iterates through the string with the structure for(i=0;s[i];i++),

If the element of the string match with the entered character c1 then replace the string element with c2 and terminate the loop.

5) Print the string after replaced the first occurrence of the character with another character.

Output:

Using Function

  1. The main() function calls the replacechar(char *s, char c1, char c2) to replace the first occurrence of the character with another character in the string.

2) For loop iterates through the string until the last character of the string becomes to null.

If the element of the string match with the entered character c1 then replace the element with the other character c2.

Then terminate the loop.

3) The main() function prints the string after replaced the first occurrence of the character with another in the string.

Output:

Using Recursion

  1. The main() calls the recursive function replacechar(char *s, char c1, char c2) by passing the string, the character wants to replace, character want to replace with as arguments to the function.

2) c1 is the character which we want to replace,c2 is the character which we want to replace with,i=0.

a) if the element s[i] of the string match with the entered character c1 then replace s[i] with c2. Then break the loop.

b) If s[i] not matched with c1 then increase the i value to go through the next element of the string.

call the function itself.

The function calls itself recursively until the element of the string match with the character c1.

3) Print the string after replacing the first occurrence of the character with another character.

Output:
Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)
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 ...