Hmm, the code for this video is available here: github.com/portfoliocourses/c-example-code/blob/main/delete_substring.c. And when I try the code with "Minecraft" and deleting "Mine" it works, I get back only "craft"... see the code below. Can you post your code in a comment here? Maybe we can look at it and figure out what's going on. :-) #include #include void delete(char string[], char substr[]); int main() { char string[] = "Minecraft"; printf("Before: %s ", string); delete(string, "Mine"); printf("After: %s ", string); return 0; } void delete(char string[], char substr[]) { int i = 0; int string_length = strlen(string); int substr_length = strlen(substr); while (i < string_length) { if (strstr(&string[i], substr) == &string[i]) { string_length -= substr_length; for (int j = i; j < string_length; j++) string[j] = string[j + substr_length]; } else i++; } string[i] = '\0'; }
@@PortfolioCourses Ah, i forgot to say that i modified the code with fgets to have a custom input per open window. I used fgets instead of scanf because scanf cant have a space. My modified code: (I added fgets and a const max) #include #include #include const int MAX = 100; void deleteComponents(char string[MAX], char delString[MAX]); int main() { char string[MAX], delString[MAX]; printf("Input: "); fgets(string, MAX, stdin); printf("Delete Input: "); fgets(delString, MAX, stdin); printf(" Before: %s", string); deleteComponents(string, delString); printf("After: %s", string); return 0; } void deleteComponents(char string[MAX], char delString[MAX]) { int i = 0; int string_length = strlen(string); int substr_length = strlen(delString); while (i < string_length) { if (strstr(&string[i], delString) == &string[i]) { string_length--; for (int j = i; j < string_length; j++) { string[j] = string[j + substr_length]; } } else { i++; } } string[i] = '\0'; }
@@rene5547 Oh OK so when you use fgets it will add a 'trailing newline' on to the end of your string. The reason it's working for "craft" is because when you enter craft as the delete input, it as a newline character on the end, which matches with the newline character at the end of the "string" char array. But when you enter "Mine" as the delete input, there is really the string Mine with the newline, and that won't match with the "Mine" in the string "Minecraft ". So when you hit enter to end your input with fgets, that enter is the newline character, and it gets tagged onto the end of the string as the last character (before the null terminator). You'll want to move the null terminator up one character to remove the newline. So like this: printf("Delete Input: "); fgets(delString, MAX, stdin); // move the null terminator up one char delString[strlen(delString) - 1] = '\0'; This article might help too: aticleworld.com/remove-trailing-newline-character-from-fgets/
Maybe i am understanding this wrong, but if i type "Minecraft" and i want to delete "Mine" it dont work but if i want to delete "craft" it work 🤷♂
Hmm, the code for this video is available here: github.com/portfoliocourses/c-example-code/blob/main/delete_substring.c. And when I try the code with "Minecraft" and deleting "Mine" it works, I get back only "craft"... see the code below. Can you post your code in a comment here? Maybe we can look at it and figure out what's going on. :-)
#include
#include
void delete(char string[], char substr[]);
int main()
{
char string[] = "Minecraft";
printf("Before: %s
", string);
delete(string, "Mine");
printf("After: %s
", string);
return 0;
}
void delete(char string[], char substr[])
{
int i = 0;
int string_length = strlen(string);
int substr_length = strlen(substr);
while (i < string_length)
{
if (strstr(&string[i], substr) == &string[i])
{
string_length -= substr_length;
for (int j = i; j < string_length; j++)
string[j] = string[j + substr_length];
}
else i++;
}
string[i] = '\0';
}
@@PortfolioCourses Ah, i forgot to say that i modified the code with fgets to have a custom input per open window.
I used fgets instead of scanf because scanf cant have a space.
My modified code:
(I added fgets and a const max)
#include
#include
#include
const int MAX = 100;
void deleteComponents(char string[MAX], char delString[MAX]);
int main() {
char string[MAX], delString[MAX];
printf("Input: ");
fgets(string, MAX, stdin);
printf("Delete Input: ");
fgets(delString, MAX, stdin);
printf("
Before: %s", string);
deleteComponents(string, delString);
printf("After: %s", string);
return 0;
}
void deleteComponents(char string[MAX], char delString[MAX]) {
int i = 0;
int string_length = strlen(string);
int substr_length = strlen(delString);
while (i < string_length) {
if (strstr(&string[i], delString) == &string[i]) {
string_length--;
for (int j = i; j < string_length; j++) {
string[j] = string[j + substr_length];
}
}
else {
i++;
}
}
string[i] = '\0';
}
@@rene5547 Oh OK so when you use fgets it will add a 'trailing newline' on to the end of your string. The reason it's working for "craft" is because when you enter craft as the delete input, it as a newline character on the end, which matches with the newline character at the end of the "string" char array. But when you enter "Mine" as the delete input, there is really the string Mine
with the newline, and that won't match with the "Mine" in the string "Minecraft
".
So when you hit enter to end your input with fgets, that enter is the newline character, and it gets tagged onto the end of the string as the last character (before the null terminator). You'll want to move the null terminator up one character to remove the newline. So like this:
printf("Delete Input: ");
fgets(delString, MAX, stdin);
// move the null terminator up one char
delString[strlen(delString) - 1] = '\0';
This article might help too: aticleworld.com/remove-trailing-newline-character-from-fgets/
@@PortfolioCourses Thank you so much! It works now.
@@rene5547 You're welcome! I actually recorded a video on this topic that I'm going to post today, so thanks for the idea. :-)