C++中如何保留字符数组中的空格

img

img

img


删除所有的小写字符'a',程序运行之后总是缺少题目要求中的空格,求帮忙调一下程序,怎么实现题目要求?

哈哈, 讲真的, 你这方法算是投机取巧了, 不删除直接输出不带a的字符串, 刷题应该能过


#include <iostream>
using namespace std;

int main(){
    string str;
    cout << "please input a character string:" << endl;
    getline(cin, str);
    
    cout << "the result after delete 'a' is:" << endl;
    for(int i = 0; str[i] != '\0'; i++){
        if (str[i] != 'a')
            cout << str[i]; 
    }
    
} 

#include<iostream>
using namespace std;
int main(){
    char zifu[50];
    cout<<"please input a character string:"<<endl;
    cin.getline(zifu, 50);
    cout<<"the result after delete 'a' is ";
    for(int t=0;zifu[t]!='\0';t++){
        if(zifu[t]!='a'){
            cout<<zifu[t];
        }
    }
    return 0;
}

直接用gets[zifu]可以读入一行也不会影响你的空格输出