我想通過一個字符串並刪除字母g,使用任何內置的函數,只有一個變量,必須是一個指針,並且不允許使用方括號。我有代碼,但它一直返回一個空字符串,而不是新的編輯字符串。c字符串複製無法清空字符串
#include <iostream>
using namespace std;
void deleteG(char *str) {
char *temp = str; //make new pointer,pointing at existing, now i have initialized and enough size.
while (*str != '\0') { //while the c-string does not reach null termination
if (*str != 'g' || *str != 'G') { // if the value of the current position is not the character g or G proceed.
*temp = *str;//copy value over
temp++;//increase count
}
str++;//increase count to next char and check again above is if does not equal g or G
}
//this should now copy the new string over to the old string overriding all characters
while (*temp != '\0') {
*str = *temp;
str++;
temp++;
}
}
int main() {
char msg[100] = "I recall the glass gate next to Gus in Lagos, near the gold bridge.";
deleteG(msg);
cout << msg; // prints I recall the lass ate next to us in Laos, near the old bride.
}
一切都不是'g'或不''G'。 –
是的,複製出g或G的字符串 –
你必須用&&代替||,否則任何事都會通過 – ZenJ