Member Avatar for Rahul47

Writing a function strreplace(s,chr,repl_chr) to replace the character 'chr' with character 'repl_chr' in a givne string s.

I have done so far with following code. Getting awkward output.

// Program to replace occurence of a character with another character in a string.

#include<stdio.h>
#include<conio.h>

void strreplace(char *,char,char);

int main()
{
    char s[10],chr,repl_chr;
    printf("\nEnter a string: ");
    scanf("%s", &s);
    printf("\nEnter character to be replaced: ");
    scanf("%s", &chr);
    printf("\nEnter replacement character: ");
    scanf("%s", &repl_chr);
    printf("\nModified string after replacement is: ");
    strreplace(s,chr,repl_chr);
   getch();
   return 0;  
}

void strreplace(char s[], char chr, char repl_chr)
{
     int i=0;
     while(s[i]!='\0')
     {
           if(s[i]==chr)
           {
               s[i]=repl_chr;
           }  
           i++; 
     }
          printf("%s",s);
}

Thanx.

i think i have an idea of whats happening , but when you say awkward output , posting the awkwardness here would help tame it :)

note : scanf() to read a character is a very bad idea.
read more from here

Member Avatar for Rahul47

note : scanf() to read a character is a very bad idea.

My Lack of knowledge . . . New to this Ocean :-P

posting the awkwardness here would help tame it :)

Awkward Output = No Output.

Funciton ain't printing anything.

ok.
try replacing the 1st scanf() , the one that reads a line , with fgets() , then replace the two scanf()'s that come after that with getchar()'s.
then youll see that it also wont work , but it should give you an idea regarding scanf()s and getchars() and stdin.
HINT : its about stuff that gets left behind in the input stream.

also , a question : in the function declaration , you mention char * but in the definition ( that is waht they are called if i remember correctly ) below the main , you write char s[] . why so ?

Member Avatar for Rahul47

you mention char * but in the definition ( that is waht they are called if i remember correctly ) below the main , you write char s[] . why so ?

As UDF is taking string as an argument thats why in declaration of function it need array pointer to be specified as one of its arguments, and in definition string s[] is supplied.

Is that where am committing blunder ?

in c , passing an array is by default pass-by-reference , you dont need the char * . char s[] in both the declaration and definition would do well enough i think.

did you try what i said above there ?

edit : whats with the getch() ? if your running from the default windows cmd, you wont need getch() . if your using a modern IDE , they provide the getch() facilty built in. if your using something old like turbo c , i suggest its time you make a shift. :)

Member Avatar for Rahul47

Ok, so here is what i did:
Replaced all that you advised. But i think I was right with char * and char s[] as replacing char * with char s[] yeilds nothing than a list of errors . .

So after replacing all the scanf's with getchar and gets, we got following output.

6cd648e88e914a936ebed9082adfb4d7

I think its working but now problem here is program dosent stop for accepting replacement character due to which a [space] is replaced by default.

the suggestion i gave included a flaw in it. and i provided a hint for it also. why do you think you got an auto-return after "enter replacement character" part ?

on a different topic : whyare you giving 1 as the character to be replaced ??? where is 1 in that string you wrote ??

Member Avatar for Rahul47

Gotacha: getchar() user for entering first character was leaving a newline in the buffer to clear it used a getchar() again before entering second character, and now it works !!

Thanx a Ton somjit.

Member Avatar for Rahul47

FYI: Am using Dev C++.

well thats better :)

and since you figured out the main cause of the problem , i think its safe to show you a different version of your code , one that doesnt need the extra getchar() .

Member Avatar for Rahul47

i think its safe to show you a different version of your code , one that doesnt need the extra getchar() .

Thanx for that polishing. . . .

#include<stdio.h>
#include<conio.h>
strreplace(char[],char,char);
int main()
{
    char s[10],chr,repl_chr;
    printf("\nEnter a string: ");
    gets(s);
    printf("\nEnter character to be replaced: ");
    chr=getchar();
    fflush(stdin);
    printf("\nEnter replacement character: ");
   repl_chr=getchar();
    printf("\nModified string after replacement is: ");
    strreplace(s,chr,repl_chr);
   getch();
   return 0;  
}
strreplace(char s[], char chr, char repl_chr)
{
     int i=0;
     while(s[i]!='\0')
     {
           if(s[i]==chr)
           {
               s[i]=repl_chr;
           }  
           i++; 
     }
          puts(s);
          return 0;

}

I have written the right code only. But it's not replacing.

If it's not replacing then it's not the right code.

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.