#include <iostream>
#include <cstring>
using namespace std;

int main() {
    const int MAX_LEN = 100; // 字符串最大长度
    char str[MAX_LEN]; // 定义字符数组
    int len = 0; // 字符串实际长度

    cout << '请输入字符串(遇到回车停止):' << endl;

    // 逐个读入字符,直到遇到回车
    char ch = getchar();
    while (ch != '\n') {
        str[len++] = ch;
        ch = getchar();
    }

    str[len] = '\0'; // 字符串末尾添加'\0'

    cout << '输入的字符串为:' << str << endl;

    return 0;
}
C++ 代码实现从标准输入读取字符串

原文地址: https://www.cveoy.top/t/topic/mZGp 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录