i need to create a small program: user enter 1st string ex: I'm a beautiful girl then he needs to replace for example 'beautiful' to 'clever'
can use just stdio.h and pointers as well
help me, pls!!!
i need to create a small program: user enter 1st string ex: I'm a beautiful girl then he needs to replace for example 'beautiful' to 'clever'
can use just stdio.h and pointers as well
help me, pls!!!
Here is the outline of an algorithm
1. Parse arguments search-string and replacement-string
2. Read input line
3. Search for search-string in input line and replace it by replacement-string
4. Output line
Step 3 will have to use handcrafted loops for string operations because the typically used functions are found in <string.h>
First, write the code and use functions in string.h as usual. Now you know which functions you need to replace.
Then, research and/or ask specific questions to do with replacing those functions.
Hope this helps.
#include <stdio.h>
#define SIZE 1000
void read_str();
main() {
char search_key;
char *ptr1;
char s1[SIZE];
ptr1 = s1;
printf("Enter your string:\n");
read_str();
printf("Enter the key word u want to change:");
scanf("%c", &search_key);
// after this i don't have idea what to do....((((((
return 0;
}
void read_str()
{
int c;
while((c = getc(stdin))!= '\n')
*ptr1++ = c;
*ptr1++ = ' ';
*ptr1 = '\0';
}
Not sure about C but I did something similar in C++. C might have a similar range of string manipulations. userInput="I am a beautiful girl" replaceString=userInput.replace(5,9,"Clever");
First, let us suppose you want just literal substitution and not something more complicated that would distinguish from "beautifulgirl" and "beautiful girl".
First, write your compare function.
Then scan your input buf and copy it to dbuf (they must be big enough);
This will change every instance of "beautiful", being a word or not.
To skip beautiful, it is enough to increment i by the length of beautiful; the dest buffer index j must be instead incremented by the length of "clever". To avoid hardencoding lengths, the following
int strlen(char *s)
{
char *p = s;
while( *s != '\0' ) s++;
return s-p;
}
should be a possible impl of strlen (change its name eventually).
Bugfixes and real code implementation are left to you (since the task is very boring indeed)