組み込み関数を使用せずに文字列gを削除し、ポインタと括弧を使用できる変数を1つだけ使用して文字列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」でもありません。 –
はい、out gまたはGを使用して文字列をコピーしてください –
||の代わりに&&を使用する必要があります。そうでない場合は何も渡されません。 – ZenJ