0

Here's a question where we need to replace all occurences of a character in a string with another new string. Given below is the question:

Write a program that replaces the occurence of a given character (say c) in a primary string (say PS) with another string (say s).

Input: The first line contains the primary string (PS) The next line contains a character (c) The next line contains a string (s)

Output: Print the string PS with every occurence of c replaced by s.

NOTE: - There are no whitespaces in PS or s. - Maximum length of PS is 100. - Maximum length of s is 10.

Below is my code:

#include<stdio.h>
int main()
{
    char ps[100],*ptr,c,s[10];

    printf("Enter any string:");
    gets(ps);

    printf("Enter the character you want to replace:");
    scanf("%c",&c);

    printf("Enter the new string:");
    fflush(stdin);
    scanf("%s",&s);

    ptr=ps;

    while(*ptr!='\0')
    {
        if(*ptr==c)
        *ptr=s;
        ptr++;
    }

    printf("Final string is:");
    puts(ps);
    return 0;
}

I am not able to replace a character with a string. It just gives me a garbage output in place of the character that I want to replace.

But, when I declare it as a character, the output is as expected. It replaces the character with another character.

Could you please help me with this?

Essam Fahmi
  • 1,920
  • 24
  • 31
akash11
  • 5
  • 2

2 Answers2

3

In C, a string is a sequence of characters, represented by the address of the first. So *ptr = s should have gotten you a warning about mismatched types from the compiler. If you want to insert a string into another, you'll need to move the other characters around to make room for it.

But your problem description does not seem to require that you make a new string, just print as if you had. So you could loop through the original string, and for each character, if it is to be replaced, print the replacement string, otherwise print the original character.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • Could improve this: "In C, a string is a sequence of characters, represented by the address of the first. " In C, a _string_ is a sequence of characters up to and includes the _null character_. A _string_ is more like an array than an address. – chux - Reinstate Monica Sep 01 '17 at 15:10
  • Thank you, but how do I change my code? Should I determine the new string length and create those many spaces after the character to be replaced? If yes, how do I do so? – akash11 Sep 01 '17 at 15:49
1

The *ptr=s; in

if(*ptr==c)
    *ptr=s;

is really assigning the base address of the character array s to the memory location being pointed to by ptr. That doesn't replace the character with string but will lead to error.

I agree with yano. It would be better to create a new character array to store the resultant string as the original array may not have enough space to hold the new one.

If the new string is result, you could do something like

for(i=j=0; ps[i]!='\0'; ++i)
{
    result[j++]=ps[i];
    if(ps[i]==c)
    {
        for(--j, k=0; s[k]!='\0'; ++k)
        {
            result[j++]=s[k];
        }
    }
}
result[j]=0;

The scanf("%s",&s); should be made scanf("%s", s); as the name of an array decays into a pointer to its first element.

It's better to avoid gets() and fflush(stdin) as Sourav Ghosh mentioned.

fgets() is safer than gets() because you can guarantee you never overflow the input string buffer.

As for fflush(stdin) being wrong, have a look here.

J...S
  • 5,079
  • 1
  • 20
  • 35