0
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
void main()
{
char a[200],n,c,b;
int i;
printf("enter the new string: ");
scanf("%20s",a);
printf("enter the character to be replaced: ");
scanf("%20s",b);
printf("enter the new character: ");
scanf("%20s",c);
for(i=0;a[i]!='\0';i++)
{
    if(b==a[i])
    {
        a[i]=c;
    }
}
printf("%s",a);
}

i have 0 errors when compiler starts to work.it is stopped working

3
Contributors
10
Replies
11
Views
4 Years
Discussion Span
Last Post by deceptikon
Featured Replies
  • 1

    I'm willing to bet that you didn't think the Enter key counts as a character in the stream. Your second scanf() reads the newline from pressing the Enter key, so you're replacing whatever is typed for `b` with `'\n'`. Unless you type multiple characters for `b`, of course. Read More

  • 1

    For future reference, both a newline and a space in the format string are unnecessary. Any single whitespace character will tell scanf() to read any and all subsequent whitespace from the stream, so technically you could do this: scanf(" %c", &b); And it'll catch any number of spaces, tabs, and … Read More

0

hey, b,c are only characters. You can't enter values in it as a string. compiler dont guess how value will be entered, but when you run it, it will crash due to segmantaion fault(I am not sure for this).

0

segmentation fault means when you are accessing a location which your program is currently not allocated.

0

hey nitin1 but there is no logical mistake here bcoz one advised me not to take gets().so i used scanf

0

read my first post. I have already answered.

0

ok nitin1 see now even i couldnot replace a string in the character

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
void main()
{
char a[200],n,c,b;
int i;
printf("enter the new string: ");
fgets(a,sizeof(a),stdin);
printf("enter the character to be replaced: ");
scanf("%c",&b);
printf("enter the new character: ");
scanf("%c",&c);
for(i=0;a[i]!='\0';i++)
{
    if(b==a[i])
    {
        a[i]=c;
    }
}
printf("%s",a);
}

i couldn't identify where the error is??? thanks for ur help

1

I'm willing to bet that you didn't think the Enter key counts as a character in the stream. Your second scanf() reads the newline from pressing the Enter key, so you're replacing whatever is typed for b with '\n'. Unless you type multiple characters for b, of course.

0

ok very much thanks to deceptikon and nitin1 thanks for ur contribution i got the output

0
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
void main()
{
char a[200],n,c,b;
int i;
printf("enter the new string: ");
fgets(a,sizeof(a),stdin);
printf("\n enter the character to be replaced: ");
scanf("\n %c",&b);
printf("\n enter the new character: ");
scanf("\n %c",&c);
for(i=0;a[i]!='\0';i++)
{
    if(b==a[i])
    {
        a[i]=c;
    }
}
printf("%s",a);
}

the final code thanks

1

For future reference, both a newline and a space in the format string are unnecessary. Any single whitespace character will tell scanf() to read any and all subsequent whitespace from the stream, so technically you could do this:

scanf(" %c", &b);

And it'll catch any number of spaces, tabs, and newlines. It'll also royally jack up your I/O logic if you're not expecting that behavior. ;)

Also note that the only specifiers that don't read leading whitespace by default are %c, %[, and %n. All of the other specifiers do the "right" thing.

Edited by deceptikon

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.