Attachment 15061
I'm new in C and can't solve this Q :confused: if someone can help me solve this thing i will be grateful:).
Printable View
Attachment 15061
I'm new in C and can't solve this Q :confused: if someone can help me solve this thing i will be grateful:).
How hard did you try?
Surely you can get as far as
Code:int main ( ) {
printf("Enter a sentence: ");
}
So post what you have.
I couldn't do more.Code:#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
void Replace(char * );
int main() {
char second[80];
char first[80];
printf("Enter a sentence : ");
gets(first);
second[80] = Replace(first);
system("PAUSE");
return 0;
}
void Replace(char * snt) {
}
So where you do prompt for the replacement word?
A useful function to call may bestrstr
You will probably need to use several built in functions using the standard c library, and as the instructions states, you will have to create a single user defined function. The best c library to start out with is the string library: C Library <string.h>
Anyways, if you were to do this in C++, I think it would be much easier, due to the C++ standard template library. But with c, you'll have to go over the built in library, most notably, probably the string library, and see how much of the library's functions will help you complete this program. The rest will be up to you.
If you try to build all your own functions, when they already exist (pre made), you are making this much harder for yourself. Although your teacher may want you to build your own functions, the instructions only say 1 user defined function.
Please DO NOT use gets()! Use fgets() instead.Code:printf("Enter a sentence : ");
gets(first);
What if the user typed in a line longer than 79 characters? It would overwrite any data that in memory after the "first" array.
Better to use a simple getchar(). "system("PAUSE")" is for Windows only and is not portable.Code:system("PAUSE");
Can i handle this job with only one function?Code:#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
void Replace(char*, char*,char* );
int main() {
char second[80];
char first[80];
char aim[20];
char New[20];
printf("Enter a sentence : ");
gets(first);
second[80] = Replace(first);
printf("Enter the word you want to replace : ");
gets(target);
printf("Enter the new word : ");
gets(New);
system("PAUSE");
return 0;
}
void Replace(char*first, char*aim, char*New ) {
}
Probably not. I looked up similar questions on Google, and it required several built in string functions to complete. As Salem mentioned, a good function to start with is strstr in string.h.
Anyways, think about how you would do this in Microsoft Excel. The underlying logic is the same, but you will have to write out the code.
Think about what you are trying to do, and work it out on paper first. What Standard Library functions do you need to use? What loops and control statements will you need.
1) Enter the string to search.
2) Enter a word to search for.
3) Search for the word.
4) If not found, ask the user for another word. Go back to Step 2 & 3.
5) Once you have found the word to replace, ask the user for the replacement word.
6) Replace the word, and display the corrected string to the user.
@cmuchhard:
I have to ask. How are you learning C? From a qualified instructor, a book, YouTube videos, or some other source?
Thank you.