Well, several things immediately stand out.
First, the variable ptr1
is declared local to main
, so the read_str()
function won't be able to access it. Second, you can replace read_str()
with a call to the fgets()
library function (which is in stdio.h, so per the terms of the assignment is fair game) and get rid of read_str()
entirely:
printf("Enter your string: ");
fflush(stdout);
if (fgets(s1, sizeof s1, stdin)) // read into s1 directly
{
// process string s1
}
else
{
// error reading string
}
Since you want to replace whole words, you'll have to allocate search_key
as an array of char, not a single char:
char search_key[SOME_SIZE_GREATER_THAN_1];
and you'll read it in the same way as the source string:
if (fgets(search_key, sizeof search_key, stdin))
...
I assume you will also need to get the replacement string as well:
char replacement[SOME_SIZE_GREATER_THAN_1];
...
if (fgets(replacement, sizeof replacement, stdin))
...
You could abstract these three operations out into a separate function, since they're all doing the same thing:
int getStr(const char *prompt, char *target, size_t targetSize)
{
printf("%s:", prompt);
fflush(stdout);
if (fgets(target, targetSize, stdin) != NULL)
return 1;
else
return 0;
}
and in your main function call it as
char s1[SIZE];
char search_key[SIZE];
char replacement[SIZE];
...
if (getStr("Enter your string", s1, sizeof s1))
{
if (getStr("Enter the word you want to replace", search_key, sizeof search_key))
{
if (getStr("Enter the replacement string", replacement, sizeof replacement))
{
// do replacement
}
}
}
As for doing the actual replacement, that's the meat of the assignment, and I'm not sure how to give you hints without giving the whole thing away. Before tackling that, try just getting the input stuff working (read the source, target, and replacement strings).
EDIT
To answer some of Jennifer's questions below, here's how the overall program should be structured:
#include <stdio.h>
#define SIZE 100
/**
* I prefer to define functions before they are used (at least within the same
* translation unit, or file); the function definition is also a declaration,
* so when the function is called later on in main(), the compiler already knows
* what the right argument and return types are supposed to be. If I defined
* the function *after* main, I would still need a declaration for it before
* I called it, and I would have to make sure the declaration and definition
* matched up. This way, it's just one less thing to worry about.
*
* It does mean that my code reads "backwards", though.
*/
int getStr(const char *prompt, char *target, size_t targetSize)
{
printf("%s:", prompt);
fflush(stdout);
if (fgets(target, targetSize, stdin) != NULL)
return 1;
else
return 0;
}
/**
* This would be a good place for a function to perform the replacement
*/
int main(void) // as of C99, just writing "main()" is no longer sufficient
{
char s1[SIZE];
char search_key[SIZE];
char replacement[SIZE];
if (getStr("Enter your string", s1, sizeof s1))
{
if (getStr("Enter the word to replace", search_key, sizeof search_key))
{
if (getStr("Enter the replacement string", replacement, sizeof replacement))
{
// do replacement
}
}
}
return 0;
}
Make sure your braces match up; the error you described sounds a lot like there was a mismatched brace (next time, copy and paste the exact error text). Type the above code in as written (minus the comments) and make sure you can get it to compile and run before doing anything else.
As for what you need to do in the section marked "do replacement", well, that's the meat of the assignment. One thing I will tell you is that if you want to store the result for later use, you will have to save it to yet another array (IOW, overwriting the original string won't work if the replacement text is longer than the source text; once you figure this out, you will understand why).
Again, it's going to be really hard to give you hints on how to do the replacement without essentially giving you the answer. You might want to work this out on paper first; write down your source, search, and target strings, and figure out the operations to find the search string in the source string (hint: you'll be looking at one character at a time in each string).
Work this assignment in several pieces; first, figure out how to read the strings from standard input (which I've done for you). Then figure out how you would search the source string for a specific character. Then figure out how you would search the source string for a sequence of characters. Once you get that far, the operations to perform the replacement should be fairly obvious.
But get the skeleton code above to build first.