C program to replace a character in a string.

Posted by Mangesh on April 01, 2018

/* Repalce character in string */

Description :

We are given a string, the character to be replace, and the new value. Our aim is to replace the character with another character when the character to be replace is found.

Logic :

We have to scan the complete string and the movement we find the string to be replace, we must replace it with the respected string.

Program :

#include<stdio.h>
#include<conio.h>
void main()
{
  int i;
  char string[100], orginal, replace;
  clrscr();

  printf("Enter the String : ");
  gets(string);
  printf("\nEnter the character to be replace from orginal string : ");
  orginal = getchar();
  getchar();
  printf("\nEnter the new value : ");
  replace = getchar();
  for (i=0;i<strlen(string);i++)
  {
     if(string[i]==orginal)
       string[i]=replace;
  }
  printf("\nThe new value of string is %s",string);
getch();
}

Output :

c program for replace character in string Executed and tested in Turbo C 3.2

Written with from Mangesh.

Related Post
1 C program for naive string matching algorithm.
2 C program to check whether the given string is palindrome or not.
3 C program to concatenate two string.
4 C program to check whether two strings are anagram or not.
5 C program to reverse the string.
6 C program to replace a character in a string.
7 C program to compare two string.
8 C program to compare two string using strcmp function.
Latest Post
1 C program to implement Queue using linked list.
2 C program for binary search tree (BST).
3 C program to search an element in linked list.
4 C program for postorder traversal in binary tree.
5 C program for preorder traversal in binary tree.