#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

Recommended Answers

All 10 Replies

Member Avatar for I_m_rude

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).

segmentation fault means?????

Member Avatar for I_m_rude

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

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

Member Avatar for I_m_rude

read my first post. I have already answered.

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

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.

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

#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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.