Here are some tips:
- You have declared a variable called 'm' but you do not use it. This means that you have something wrong or you have forgot something, cause otherwise you wouldn't have included the 'm' variable.
-Supply more buffer for the sentences. Increase the size from 23 to let's say 50... Also, it is not a good idea to make the number of characters for the output array fixes because you do not know how many characters it will eventually have.. the word to replace one word from the sentence might be very big and even worse it might be replaced in many places.
- Change the numbers 23, 12, ... to #defines:
#define SENTENCE_MAX_LEN 50
-Change names s1, s2, s3... to for example:
char sentence[ SENTENCE_MAX_LEN ];
-Do not ever use gets()!!! Why? Because it does not check how many characters it receives and it might lead to memory leaks. Especially for your program, this is very easy. When you ask for a sentence, i just put a sentence. But most sentences are more than 22 characters... your program does not check this. If you are lucky you will get an assertion or something like that.
You must check the number of characters you receive to store them in the array. You use fgets() like this:
fgets( sentence, SENTENCE_MAX_LEN, stdin );
which says read up to one less than SENTENCE_MAX_LEN characters from stdin and store them in sentence. Also, put a '\0' character at the end.
Till that point your program can be restructured like that:
-
#include <stdio.h>
-
-
#define SENTENCE_MAX_LEN 50
-
#define WORD_MAX_LEN 15
-
-
int main( void ){
-
-
char sentence[ SENTENCE_MAX_LEN ];
-
char word[ WORD_MAX_LEN ];
-
-
fprintf( stdout, "\nPlease enter one sentence. Note that i will accept only
-
%d characters!\n", SENTENCE_MAX_LEN - 1 );
-
fflush( stdout );
-
fgets( sentence, SENTENCE_MAX_LEN, stdin );
-
/* you might also want to get rid of unwanted-extra input here... */
-
-
fprintf( stdout, "\nNow give me the word you want me to replace. Note that i
-
will accept only %d characters!\n",
-
WORD_MAX_LEN - 1 );
-
fflush( stdout );
-
fgets( word, WORD_MAX_LEN, stdin );
-
/* you might also want to get rid of unwanted-extra input here... */
-
-
....
-
-
return ( 0 );
-
-
}
-
- Do not put everything in a big for loop. This is not the purpose of structural programming. Divide this thing in small routines that you can check, debug, modify and extend easily. What you must do is:
Find what does each section of your for loop accomplishes.
Make each task to a separate routine.
-Your for loop will execute meaninglessly even if the word in not found in the sentence. Check for that too.
To continue from above you can do:
-
fprintf( stdout, "\nI am searching for the word \"%s\" you want me to replace...\n", word );
-
-
occurrences = searchWord( sentence, word );
-
-
if ( occurences > 0 ){
-
fprintf( stdout, "\nI found the word %s %d times in the sentence.\n",
-
word, occurrences );
-
-
replaceWord( sentence, word );
-
-
}
-
else{
-
-
fprintf( stdout, "\nSorry but i did not found the word in the sentence... Goodbye!%c", '\n' );
-
return ( 1 );
-
-
}
-
-When you output, you say: s4=%s.
This is really, really bad. What the hell is s4??? How the hell will a user running the program know what s4 is? He does not know how you name your variables!!! Probably he does not know that behind the program there is source code!!!!!!!!!!!!
Make a friendly user interface. Put nice messages, like:
For input:
Type some text. I will only receive %d characters!
Please enter the word you want me to locale in the sentence and replace it with another word.
The word is not located in the sentence. Please enter again.
There are %d occurrences of the word in the sentence. Enter the word that you want to replace word %s.
For output:
The sentence with the word(s) replaced is: %s. ---> you do this like that:
-
fprintf( stdout, "\nThe sentence with the %s replaced is: %s\n",
-
occurrences == 1 ? "word" : "words",
-
newSentence );
-
Personally, i suggest that you divide in its own routine even the part (in the code above i show you) that reads input. Assign the correct responsibilities to each of the routines.
Let us know how you progress!!