Delete All Occurrences Of A Substring From A String | C Programming Example

  Рет қаралды 12,129

Portfolio Courses

Portfolio Courses

Күн бұрын

Пікірлер
@rene5547
@rene5547 2 жыл бұрын
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 🤷‍♂
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
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'; }
@rene5547
@rene5547 2 жыл бұрын
@@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'; }
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
@@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/
@rene5547
@rene5547 2 жыл бұрын
@@PortfolioCourses Thank you so much! It works now.
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
@@rene5547 You're welcome! I actually recorded a video on this topic that I'm going to post today, so thanks for the idea. :-)
String In Char Array VS. Pointer To String Literal | C Programming Tutorial
9:58
“Don’t stop the chances.”
00:44
ISSEI / いっせい
Рет қаралды 62 МЛН
Quando A Diferença De Altura É Muito Grande 😲😂
00:12
Mari Maria
Рет қаралды 45 МЛН
She made herself an ear of corn from his marmalade candies🌽🌽🌽
00:38
Valja & Maxim Family
Рет қаралды 18 МЛН
Why You Shouldn't Nest Your Code
8:30
CodeAesthetic
Рет қаралды 2,8 МЛН
Create Your Own strcpy() String Copy Function | C Programming Example
10:33
My 2 Year Journey of Learning C, in 9 minutes
8:42
VoxelRifts
Рет қаралды 673 М.
Remove Spaces from String | Logical Programming in C | Naresh IT
7:57
Naresh i Technologies
Рет қаралды 42 М.