我想通过一个字符串并删除字母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