Turn Desktop View Off
by Dinesh Thakur

In this program user asks to replace the string from existing one. User declares required char type variables that will be used to store the value. Here in operation mode user declare nested for loop statement in that statement user elaborate the variables that are used to functionality mode. Here in other type mode user also use if condition along within do while loop. If (l1>=l2) condition got true than do while loop will come in function. In do while condition for loop also exists for replacing the character and an if-else condition too. Condition going to be check that if the character exists in the string or not the condition got true then it will replace the character. And display the result on the screen.

Problem statement:
This is C Program that asks user to replace the character from the existing one.

  1. Declaring the variable.
  2. Using while condition.
  3. Using loop statement.
  4. Display the result on screen.

Here is C source code for replacing the character from string. The output of this program shown below. 

#include<stdio.h>

#include<conio.h>
void main()
{
         char a[100],b[100],c[100],d[100];
         int i,j,k,l1,l2,l3,m,count=-1;
         clrscr();
         printf("\t\t\t   REPLACE OF STRING ");
         printf("\n\t\t\t********************");
         printf("\n\n\t INPUT");
         printf("\n\t*******");
         printf("\n\nEnter the First String : ");
         scanf("%[a-z ]s",a);
         printf("\nEnter the Substring : ");
         scanf("%s",b);
         printf("\nEnter the Replace String : ");
         scanf("%s",c);
         printf("\n\n\tOUTPUT");
         printf("\n\t*******");
         for(i=0;a[i]!='\0';i++)
              l1=i;
              for(i=0;b[i]!='\0';i++)
                   l2=i;
                   for(i=0;c[i]!='\0';i++)
                        l3=i;
                        for(i=0;a[i]!='\0';i++)
                             d[i]=a[i];
                             d[i]='\0';
                             if(l1>=l2)
                              {
                                 do
                                   {
                                       for(i=0,j=0;a[i]!='\0' && b[j]!='\0';i++ )
                                           {
                                                 if(a[i]==b[j])
                                                   {
                                                      count++;
                                                      j++;
                                                    }
                                                   else
                                                    {
                                                            count=-1;
                                                             if(j>0)
                                                                i--;
                                                                j=0;
                                                     }
                                              }
                                                                m=i;
                                                     if(count==l2)
                                                       {
                                                            i=i-j;
                                                            for(k=0;c[k]!='\0';i++,k++)
                                                                  a[i]=c[k];
                                                                  if(l2!=l3)
                                                                    {
                                                                        for( ;d[m]!='\0';m++,i++)
                                                                             {
                                                                                  a[i]=d[m];
                                                                              }
                                                                     }
                                                                                  //a[i]='\0';
                                                                                  //printf("\nReplaced string:%s",a);
                                                          }
                                                                                   else
                                                                                   break;
                                                                                   //printf("\nThe substring is not there");
                                     }while(a[i]!='\0');
                                       printf("\nReplaced string:%s",a);
                               }
                                       getch();
}

C Program For Replace a String