C Program to Replace Character from a String

C Program to Replace Character from a String .Compiled in Dev C++

#include<stdio.h>
#include<string.h>
#include<conio.h>
void ReplaceCharacter(char [],char ,char );
main()
{
      char text[50],a,b;
       
       
      printf("\nProgram to Replace Character from a String \n");
      printf("\n----------------------------------------------\n\n\n");
      printf("Enter Text : ");                                                   
      gets(text);
      
      
     printf("\n\nEnter the character you want to replace and by what? : ");
     scanf("%c %c",&a,&b);
      
      ReplaceCharecter(text,a,b);
      
      printf("\n\nNew Word is  %s" ,text);
      
      getch();
}

 void ReplaceCharacter(char source[],char to_be_replace,char replacewith)
 {
      char *p;
      int i;
      
      for(i=0;i<strlen(source);i++)
      {
           p=&source[i];
           
           if(*p==to_be_replace)
           {
                source[i]=replacewith;
                                  
           }
           
      }
      
 }
       

Output

replacechar

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s