用C++的string类完成如下题目 描述 从键盘输入一个字符串,字符串中含有一个空格,根据空格将字符串分割为两个字符串。 输入描述 1)输入字符串的长度不超过1000; 2)输入的字符串中含有一个空格; 3)根据空格将字符串分割为两个字符串,分别保存在两个字符数组中; 输出描述 1)输出分割完成的两个字符串; 用例输入 1 abc 123 用例输出 1 abc 123
时间: 2023-06-23 16:09:58 浏览: 44
以下是C++的代码实现:
```cpp
#include <iostream>
#include <string>
using namespace std;
int main()
{
string input;
getline(cin, input); // 从键盘输入一行字符串
int index = input.find(' '); // 找到空格的索引位置
string str1 = input.substr(0, index); // 提取第一个子字符串
string str2 = input.substr(index + 1); // 提取第二个子字符串
cout << str1 << " " << str2 << endl; // 输出分割完成的两个字符串
return 0;
}
```
首先使用 `getline()` 函数从键盘输入一行字符串,然后使用 `find()` 函数找到第一个空格的索引位置,再使用 `substr()` 函数提取出空格前后的两个子字符串,最后输出这两个子字符串即可。
相关问题
c++从键盘输入一个字符串,删除字符串中的所有空格
### 回答1:
可以使用循环遍历字符串,如果遇到空格则跳过,否则将字符加入到新的字符串中。
例如:
```c
#include <stdio.h>
int main() {
char str[100], new_str[100];
int i, j = 0;
printf("请输入字符串:");
scanf("%[^\n]", str); // 读取一行字符串,遇到换行符结束
for (i = 0; str[i] != '\0'; i++) {
if (str[i] != ' ') {
new_str[j] = str[i];
j++;
}
}
new_str[j] = '\0'; // 添加字符串结尾标志
printf("删除空格后的字符串为:%s\n", new_str);
return 0;
}
```
输入:
```
请输入字符串:Hello world!
```
输出:
```
删除空格后的字符串为:Helloworld!
```
### 回答2:
要删除字符串中的所有空格,可以按照以下步骤进行操作。
首先,从键盘输入一个字符串,可以使用input函数来实现。例如,我们可以使用以下代码来实现输入操作:
```
string = input("请输入字符串:")
```
接下来,我们使用replace函数将字符串中的空格替换为空字符。replace函数可以将一个指定的字符或字符串替换为另一个字符或字符串。在这个例子中,我们需要将空格替换为空字符,所以我们将空格作为第一个参数,空字符作为第二个参数。代码如下所示:
```
string = string.replace(" ", "")
```
最后,我们可以输出修改后的字符串,以便查看结果。使用print函数来实现这一点。代码如下所示:
```
print("删除空格后的字符串为:", string)
```
综上所述,我们可以将上述代码组合在一起,如下所示:
```
string = input("请输入字符串:")
string = string.replace(" ", "")
print("删除空格后的字符串为:", string)
```
这样,我们就可以从键盘输入一个字符串,并删除其中的所有空格。输出的结果是删除空格后的字符串。
### 回答3:
要删除字符串中的所有空格,可以使用以下方法:
1. 获取用户输入的字符串:
```
input_str = input("请输入一个字符串:")
```
2. 使用字符串的 `replace()` 方法将所有空格替换为空字符:
```
new_str = input_str.replace(" ", "")
```
3. 输出删除空格后的新字符串:
```
print("删除空格后的字符串为:", new_str)
```
完整代码如下:
```
input_str = input("请输入一个字符串:")
new_str = input_str.replace(" ", "")
print("删除空格后的字符串为:", new_str)
```
用户输入的字符串中的所有空格都会被替换为空字符,并输出删除空格后的新字符串。
用C++string类作为形参统计从键盘输入的字符串中字母、数字、空格和其他字符数的个数
以下是用C++的string类作为形参统计从键盘输入的字符串中字母、数字、空格和其他字符数的个数的示例代码:
```cpp
#include <iostream>
#include <string>
using namespace std;
void countChars(string str, int& letters, int& digits, int& spaces, int& others) {
letters = digits = spaces = others = 0;
for (int i = 0; i < str.length(); i++) {
char ch = str[i];
if (isalpha(ch)) letters++;
else if (isdigit(ch)) digits++;
else if (isspace(ch)) spaces++;
else others++;
}
}
int main() {
string input;
cout << "Enter a string: ";
getline(cin, input);
int letters, digits, spaces, others;
countChars(input, letters, digits, spaces, others);
cout << "Letters: " << letters << endl;
cout << "Digits: " << digits << endl;
cout << "Spaces: " << spaces << endl;
cout << "Others: " << others << endl;
return 0;
}
```
在这个示例中,`countChars`函数接受一个`string`类型的参数`str`,以及四个引用类型的参数`letters`、`digits`、`spaces`、`others`,用于统计字符串中字母、数字、空格和其他字符的个数。在函数中,首先将这四个参数的值初始化为0,然后遍历字符串中的每个字符,根据字符的类型将相应的计数器加1。最后,在`main`函数中,使用`getline`函数从标准输入中读取一行字符串,并将其作为参数传递给`countChars`函数进行统计。最后,输出统计结果。