Q: How to get inputs accepted in this program? I can't take inputs except string.
If I give inputs in code itself, program is working.
But when I try to take inputs, its not working.
How do I get it to accept given inputs?
For example, if s were "codebook", and from == 'o' and to == 'e', s would become "cedebeek".
#include<stdio.h>
#define MAX 50
void replace(char *s, char from, char to)
{
  int i=0;
  while(s[i]!='\0')
   {
      if(s[i]==from)
       {
          s[i]=to;
       }
      i++;
   }
}
int main()
{
  char str[MAX];
  char from;
  char to;
  printf("Enter the string");
  scanf("%[^\n]s",&str[0]);

  printf("\nEnter the character to be replaced");
  scanf("%c",&from);

  printf("\nEnter the character to be replaced with");
  scanf("%c",&to);

  replace(str, from, to);
  printf("\nThe modified string is %s",str);

  return(0);
}


A: There are a couple of problems in the posted code. Since arrays decay to pointers to their first elements in most expressions, there is no need for the address operator & in the call to scanf(); also, the trailing s is not part of the scanset conversion specifier:
scanf("%[^\n]", str);

As pointed out by @M.M in the comments, it is not incorrect to use &str[0] here instead of str, but it is more idiomatic, and I personally find it more clear, to use the less cluttered str.
When scanf() returns, a newline character will be left in the input stream, so you should add a leading space in the next call to scanf() to skip over this \n character before reading the user input:
scanf(" %c",&from);

And this call to scanf() will also leave a \n character in the input stream, so again:
scanf(" %c",&to);

Note that you should really specify a maximum width to avoid buffer overflow when reading user input into a string; there is no easy way to do this with MAX, but you can do:
scanf("%49[^\n]", str);

You could further improve code and ensure that input is as expected by checking the values returned by the calls to scanf().
