Need some help on a char swap
Hello all,
This is my last hmwk problem, and I'm a little confused with what to do with it.
-I need to write a recursive function that changes all occurences of character t in s (string) to character r (replace).
-For example: if s (string) were "priority", t=='i', and r=='y', s would become "pryoryty".
(I am asking the user for an input string, t, and r.)
Here is what I have so far. I understand that this function needs to be recursive, but I was trying to just get it working 1st. If anyone can tell me what is wrong, or what I need to adjust please let me know.
Also, anyone who can guide me in transforming the function into a recursive one would be a major help. Recursion is making my head hurt since it was thrown at us in the last half of our most recent lecture. Thanks guys.
Code:
#include<stdio.h>
#include<string.h>
#define SIZE 15
void replace_r(char *s, char c, char r)
{
char *p;
for (p=s; *p; p++) {
if (*p==c){
*p = r;
}
}
}
main()
{
char original_str[SIZE], s_char[1], r_char[1];
printf("Original word:");
gets(original_str);
printf("c(original character)=:");
gets(s_char);
printf("r(replace)=:");
gets(r_char);
replace_r(original_str, s_char, r_char);
printf("New Sentence: %s\n", original_str);
}