Help with replacing letters

Hi, I have a small problem with my program, what it does is it asks the user to input a name, lets say "summer", then it asks the user to input a letter to find, then it asks what to replace it with. If i choose "m" to find and "p" to replace it works fine but one small problem. the out put has two lines, "supmer" and the second line would be "supper". How can I change it to only show one line and it would be only "supper"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdio.h>

int find_first_char(char *str, char find)
{
 int i=0;


 while(str[i] != '\0')
 {
        if (str[i] == find)
        {
                return(i);
        }
 i++;
 }
  return(-1);
}

int main(void)
{
 int res;
 char find, replace, junk;
 char string[100];

 printf("input string to search: ");
 scanf("%s%c", string, &junk);

 printf("input character to find: ");
 scanf("%c%c", &find, &junk);

 printf("input character to replace: ");
 scanf("%c%c", &replace, &junk);

 res = find_first_char(string, find);


 while (res !=  -1)
 {

 string[res] = replace;
 res = find_first_char(string, find);

 printf(string);
 printf("\n");
 }



 return(0);
}

Don't print inside the loop

scanf("%s%c", string, &junk); ¿what is that for? (junk I mean)
Thanks and I think its for the char. someone told me when you make a char character you need to add the junk because it reads two things, the letter and trash. didnt really understand it too well...
Last edited on
Topic archived. No new replies allowed.