C Program to Replace First Occurrence of a Character in a String

Write a C Program to Replace First Occurrence of a Character in a String with example.

C Program to Replace First Occurrence of a Character in a String Example 1

This C program allows the user to enter a string (or character array), and a character value. Next, it will search and replace the first occurrence of a character inside a string.

/* C Program to Replace First Occurrence of a Character in a String */
 
#include <stdio.h>
#include <string.h>
 
int main()
{
  	char str[100], ch, Newch;
  	int i;
 
  	printf("\n Please Enter any String :  ");
  	gets(str);
  	
  	printf("\n Please Enter the Character that you want to Replace :  ");
  	scanf("%c", &ch);
  	
  	getchar();
  	
  	printf("\n Please Enter the New Character :  ");
  	scanf("%c", &Newch);
  	
  	for(i = 0; str[i] != '\0'; i++)
  	{
  		if(str[i] == ch)  
		{
  			str[i] = Newch;
  			break;
 		}
	}
	printf("\n The Final String after Replacing First occurrence of '%c' with '%c' = %s ", ch, Newch, str);
	
  	return 0;
}
C Program to Replace First Occurrence of a Character in a String 1

First, we used For Loop to iterate every character in a String.

for(i = 0; i <= strlen(str); i++)
{
	if(str[i] == ch)  
	{
		str[i] = Newch;
		break;    	
	}
}

str[] = Hello World
ch = l
Newch = @

For Loop First Iteration: for(i = 0; i <= strlen(str); i++)

Within the C Programming While Loop, we used If statement to check whether the str[0] is equal to a user-specified character or not

if(str[i] == ch) => if(H == l)

The above condition is false. So, i will increment, and Flag is still 0.

Second Iteration: for(i = 1; 1 <= 11; 1++)

if(str[i] == ch) => if(e == l) –  condition is false. So, i will increment, and Flag is still 0.

Third Iteration: for(i = 2; 2 <= 11; 2++)
if(str[i] == ch) => if(l == l) –  condition is True
str[i] = Newch
str[2] = @
and, break statement will exit from the loop

Next, we used the printf statement to print the final string.

printf("\n The Final String after Replacing First occurrence of '%c' with '%c' = %s ", ch, Newch, str);

C Program to Replace First Occurrence of a Character in a String Example 2

This program to replace the first character occurrence is the same as above. Here, we just replaced the For Loop with While Loop.

/* C Program to Replace First Occurrence of a Character in a String */
 
#include <stdio.h>
#include <string.h>
 
int main()
{
  	char str[100], ch, Newch;
  	int i = 0;
 
  	printf("\n Please Enter any String :  ");
  	gets(str);
  	
  	printf("\n Please Enter the Character that you want to Replace :  ");
  	scanf("%c", &ch);
  	
  	getchar();
  	
  	printf("\n Please Enter the New Character :  ");
  	scanf("%c", &Newch);
  	
  	while(i <= strlen(str))
  	{
  		if(str[i] == ch)  
		{
  			str[i] = Newch;
  			break;
 		}
 		i++;
	}
	printf("\n The Final String after Replacing First occurrence of '%c' with '%c' = %s ", ch, Newch, str);
	
  	return 0;
}
 Please Enter any String :  tutorial gateway

 Please Enter the Character that you want to Replace :  t

 Please Enter the New Character :  M

 The Final String after Replacing First occurrence of 't' with 'M' = Mutorial gateway

Program to Replace First Occurrence of a Character in a String Example 3

This program to replace first character occurrence is the same as the first example. Still, this time, we used the Functions concept to separate the logic.

/* C Program to Replace First Occurrence of a Character in a String */
 
#include <stdio.h>
#include <string.h>

void Replace_FirstOccurrence(char *str, char ch, char Newch);
 
int main()
{
  	char str[100], ch, Newch;
  	int i = 0;
 
  	printf("\n Please Enter any String :  ");
  	gets(str);
  	
  	printf("\n Please Enter the Character that you want to Replace :  ");
  	scanf("%c", &ch);
  	
  	getchar();
  	
  	printf("\n Please Enter the New Character :  ");
  	scanf("%c", &Newch);
  	
  	Replace_FirstOccurrence(str, ch, Newch);
  	
  	printf("\n The Final String after Replacing First occurrence of '%c' with '%c' = %s ", ch, Newch, str);
	
  	return 0;
}

void Replace_FirstOccurrence(char *str, char ch, char Newch)
{
	int i;

	for(i = 0; str[i] != '\0'; i++)
	{
		if(str[i] == ch)
		{
			str[i] = Newch;
  			break;
		}  
	}
}
 Please Enter any String :  learn c programming

 Please Enter the Character that you want to Replace :  e

 Please Enter the New Character :  @

 The Final String after Replacing First occurrence of 'e' with '@' = l@arn c programming