First: the only string function there is strlen which is easily implementable. If you cant' use the strings library, you'll implement each of the function you'll use.
Consider a string like "HELLO". In memory it'll be like
Code:
H E L L O 0
| | | | | |
index: 0 1 2 3 4 5
Now think how you can, given any string, to find the null char that ends the string? You''l have to use a loop. You'll also need a strcpy. Using the representation above, instead of just counting chars, assign them to a new string till the 0 is found.
Second:
Code:
while(gets(userstr) && strlen(userstr) != 0)
CHANGE TO
while(fgets(stdin,100,userstr) && strlen(userstr) <=1)
to prevent buffer overflow. Imagine that the user inserts more than 100 characters... Yes no place in string. And strlen(...)<=1 because fgets stores newlines ('\n').
Third, the function to replace may receive a string and send another back with the needed changes:
Code:
void change_words(char* dest, const char* src){
int index=0, index;
loop:
-check if chars at positions index,index+1,index+2 are 'm''a''n'
-if so copy to dest "person", and move dest pointer 5 positions ahead
-if not check if chars at positions index,index+1,index+2 are 'M''a''n'
-if so copy to dest "Person", and move dest pointer 5 positions ahead
-if not, no match was found, so place in dest, the char from src in position index,
and move the dest pointer to next position
-continue loop if index is not in the end of src, increment index
}
If you don't know what I mean bit change the position of a pointer, consider again the little representation of the string above.
Code:
char *str="HELLO";//a)
str++;//b)
str+=4;//c)
In a) dereferencing str ( *str) results in char 'H'
In b) dereferencing str ( *str) results in char 'E', because the ponter was moved to the next char
In c) dereferencing str ( *str) results in 0, the terminating character, because the pointer is moved 4 chars ahead, to the end of the string.
All of these chars could have been read with calls to str[0], str[1] and str[5].
If you're stiil reading this, go to this link please