-3

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.

Test Case-

1) Input:

abcxy
b
gh

Output:- aghxy

2) Input:

Al@bal#20owL
l
LL

Output:- ALL@baLL#20owL

This is the code I wrote:

#include<stdio.h>
#include<string.h>
int main()
{
char PS[105];
char Final[105];
char ch;
int i;
fgets(PS, sizeof(PS), stdin);
scanf("%c",&ch);
scanf("%s",Final);
for(i=0;i<strlen(PS);i++)
    {
    if(PS[i]==ch)
    {
    for(i=0;i<strlen(Final);i++)
    printf("%c",Final[i]);
    }
        else
    printf("%c",PS[i]);
    }


return 0;

}

  • try to google: why gets is dangerous. – Jean-François Fabre Sep 05 '17 at 13:14
  • I just edited it.. even with fgets , the error come is this In function main: test.c:10:2: error: too few arguments to function fgets fgets(PS); ^ In file included from test.c:1:0: /usr/include/stdio.h:622:14: note: declared here extern char *fgets (char *__restrict __s, int __n, FILE *__restrict __stream) ^ – SrikantShrekk Sep 05 '17 at 13:19
  • Hey Jean ... I tried with fgets too but I told in the above comment the error I get. – SrikantShrekk Sep 05 '17 at 13:20
  • 1
    "I tried with fgets"-- [read the documentation](http://port70.net/~nsz/c/c11/n1570.html#7.21.7.2) for the [functions that you use](http://man7.org/linux/man-pages/man3/fgets.3.html); don't guess how they work. – ad absurdum Sep 05 '17 at 13:32
  • i finally replaced gets(PS) with fgets(PS, sizeof(PS), stdin); . my first test case passed but in the 2nd test case it went on to a infinite loop "AL@baL@baL@baL@baL ...." and that too it is in loop considereing the strings before # .. I hope u got it.. – SrikantShrekk Sep 05 '17 at 13:41
  • @Jean-FrançoisFabre : Why don't you wait on earth to sort the problem out by asker before voting it as a duplicate. There's always a second face to coin. Not all are same. – SrikantShrekk Sep 05 '17 at 13:49
  • because it doesn't add anything useful to the site. Same questions and same answers (possibly good) all over again. – Jean-François Fabre Sep 05 '17 at 13:50
  • @Jean-FrançoisFabre : I faced a new problem. I already posted. Can you help me out? – SrikantShrekk Sep 05 '17 at 13:52
  • 1
    If you have a new problem, post it as a separate question. – dbush Sep 05 '17 at 13:55
  • @SrikantShrekk-- don't modify your question when you have a new question; post a new question (and maybe search SO for duplicates first). – ad absurdum Sep 05 '17 at 13:55
  • @DavidBowling : If I do that the first thing people will do it mark as duplicate. It's pity. There's an option to modify which will let users know what I did, and probably people won't mark me fake. – SrikantShrekk Sep 05 '17 at 13:58
  • @dbush : I'm not pointing this at you but because of some non-patient people, after marking it duplicate crates the problem for the asker. Now, for the next 7 days I can't ask a new question just because they gave one advice and marked duplicate. I understand few guys are good at coding but doesn't mean in a minute you'll mark as duplicate. TRY TO UNDERSATND. I edited. sorted out. But now I cant post new question. Atleast give a breathe to the asker to think and edit. – SrikantShrekk Sep 05 '17 at 14:03

1 Answers1

2

The gets function is dangerous because it places no limit on the number of characters entered.

For example given the following code:

char str[10];
gets(str);

If the user were to enter a string 10 characters or longer, the result would be written off the end of the array. Doing so invokes undefined behavior.

A safer alternative is the fgets function which has the following signature:

char *fgets(char *s, int size, FILE *stream);

The first parameter is the buffer to receive the string, and the second is the size of that buffer. In your case you would call it like this:

fgets(PS, sizeof(PS), stdin);

Note that unlike gets, fgets will store a newline character in the resulting string if there is space for it.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
dbush
  • 205,898
  • 23
  • 218
  • 273
  • Thank you @dbush @ TobySpeight .. Your above comment helped me for my first test case but in the 2nd test case it went on to a infinite loop "AL@baL@baL@baL@baL ...." and that too it is in loop considering the strings before # .. I hope u got it. – SrikantShrekk Sep 05 '17 at 13:54