I want to make a function which will replace two strings without using <string.h> library. In order to achieve that, I used 6 manually written functions which are together achieving this task.
I have a small problem with this code, this doesn't check if string which will be replaced is word which is completely equal to replacing string.
For example:
char s[] = "Why is ostring in C so hard",
change_what[] = "string",
into_what[] = "pointers";
OUTPUT should be:
"Why is ostring in C so hard"
Because "ostring" is not completely equal to "string".
My output is:
"Why are opointers in C so hard"
Code:
#include <stdio.h>
int compare(char *x, char *y)
{
while (*x != '\0' || *y != '\0')
{
if (*x == *y)
{
x++;
y++;
}
// if they are not equal
else if ((*x == '\0' && *y != '\0') || (*x != '\0' && *y == '\0') ||
*x != *y)
{
return 0;
}
}
return 1;
}
int lenght(char *a)
{
char *b;
for (b = a; *a; a++)
;
return a - b;
}
char *substring(char *main_string, char *substring) {
while (*main_string != '\0') {
char *p = main_string;
char *q = substring;
while (*p++ == *q++) {
if (*p == ' ' || *p == '\0')
if (*q == '\0') {
return main_string;
}
}
main_string++;
}
return NULL;
}
void replace_string_add(char *s, char *change_what, char *into_what,
int shift)
{
char *i_pointer = into_what;
char *c_pointer = change_what;
char *position = substring(s, change_what);
while (position != NULL)
{
char *end = position;
while (*end != '\0')
{
end++;
}
while (end > position)
{
*(end + shift) = *end;
end--;
}
while (*into_what != '\0')
{
*position++ = *into_what++;
}
position = substring(s, change_what);
into_what = i_pointer;
change_what = c_pointer;
}
}
void replace_string_remove(char *s, char *change_what, char *into_what,
int shift)
{
char *i_pointer = into_what;
char *c_pointer = change_what;
char *position = substring(s, change_what);
while (position != NULL)
{
char *temp = position;
while (*(temp + shift) != '\0')
{
*temp = *(temp + shift);
temp++;
}
*temp = '\0';
while (*into_what != '\0')
{
*position++ = *into_what++;
}
position = substring(s, change_what);
into_what = i_pointer;
change_what = c_pointer;
}
}
void replace_string(char *s, char *change_what, char *into_what)
{
int shift = lenght(into_what) - lenght(change_what);
if (compare(change_what, into_what) == 0)
{
if (shift >= 0)
{
replace_string_add(s, change_what, into_what, shift);
}
else
{
replace_string_remove(s, change_what, into_what, -shift);
}
}
}
int main()
{
char s[] = "Why is ostring in C so hard",
change_what[] = "string",
into_what[] = "pointers";
replace_string(s, change_what, into_what);
printf("\"%s\"", s);
return 0;
}
If string is "Why is strings in C so hard" program would work correct because it checks if last characters are ' ' or '\0'.
"Why is ostring in C so hard" wouldn't work, because it doesn't check first character.
Could you help me modify this code to check also first character?
- Note: auxiliary strings and dynamic allocation are not allowed
string.his not a library. It is a header which declares functions that are in the standard library.char s[1024]="Why are strings in C so hard"to avoid overflowing the buffer. And add bounds checking.sinmainhas space for exactly 29 characters including the terminator, your 'replacement' will result in a string 30 characters long including the terminator. E.g. you're trying to stuff 30 pounds of nuts in a 29 pound bag. You need to changesto allow for the extra required space.