Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Hi.. i need help with a code that replace word entered in a string. Pls tell the mistake or share some other code?

+3 votes
asked Feb 26, 2021 by Kashish Yusuf (150 points)
#include <stdio.h>
#include<string.h>
int main(void) {int len;
 char str[100],d[100][100],a[100],b[100];
printf("enter the string\n");
scanf("%[^\n]s",str);
len=strlen(str);
int i=0;int j=0;int k;
for(;i<len;i++)
{if(str[i]==' ')k=0;
  if(str[i]!=' ')
{d[j][k]=str[i];k++;
if(str[i+1]==' ')
j++;}}
printf("enter the word to be replaced\n");
scanf("%s",a);
printf("enter the replaced word\n");
scanf("%s",b);
int l=0;
for(;l<=j;l++)
if(strcmp(a,d[l])==0)
strcpy(d[l],b);
printf("the new string is as folows\n");
int w=0;
for(;w<=j;w++)
printf("%s ",d[w]);
  return 0; }

1 Answer

0 votes
answered Feb 27, 2021 by xDELLx (10,500 points)
edited Feb 28, 2021 by xDELLx

Please add comments in the code you share ,it will help make things clearer to others.

Always indent your code ,its easy to look at blocks of structured code & make sense of the code.

Always provide initial values to variables ,C language may have garbage initial values to  uninitialised variables.

I could not  understand why below code was written,but has few issues


int i=0;int j=0;int k;    //K can have junk values & could give 
                         //funny results when used as index in array :)
for(;i<len;i++)
{if(str[i]==' ')k=0;    //if source string has spaces , k=0 
                        //but what if no spaces are ever found ??
  if(str[i]!=' ')      
{d[j][k]=str[i];k++;   //possible case of garbage value of K read & incremented
if(str[i+1]==' ')      
j++;}}


Assuming the word is always a single character & we have to replace all the occurences of that character with a user input character ,below is what i did.


#include <stdio.h>
#include <string.h>
int main(void) {
        int len;
        char str[100],d[100][100]; //,a[100],b[100];
        char a=' ';
        char b=' ';
        printf("enter the string\n");
        scanf("%[^\n]s",str);
        len=strlen(str);
        int i=0;int j=0;int k;
        /*  
        for(;i<len;i++)
        {
                if(str[i]==' ') k=0;
                if(str[i]!=' ')
                {
                    d[j][k]=str[i];k++;
                    if(str[i+1]==' ')
                                j++;
                }
        }
        */
        printf("enter the word to be replaced\n");
        scanf(" %c",&a);
        printf("enter the replaced word\n");
        scanf(" %c",&b);
        int l=0;
        /*for(;l<=j;l++)
                if(strcmp(a,d[l])==0)
                        strcpy(d[l],b);
        */
        for(i=0;i<len;i++)
        {   
            if(str[i]==a)
            {   
                str[i]=b;   
            }
        }   
        printf("the new string is as folows\n");
        printf("%s ",str);
        return 0;
}
/*
output
enter the string
sttttringgg
enter the word to be replaced
t
enter the replaced word
x
the new string is as folows
sxxxxringgg 
*/
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...