本关任务:在一串隐藏着一个或多个数值的字符中,选出字符串中的所有数字字符,并将选出的数字字符重新组成新字符串。如果在第一个数字字符之前有负号,则保留该负号,有多个负号时只保留一个。 例如:输入的字符串为“a-1-2-4sd5 s6”,抽取数字后得到的新字符串为“-12456”。给出完整代码#include <iostream> using namespace std; #include <cstring> void extractNum(char * str); int main() { char s[1024]; cin.getline(s,1024); // 输入一行字符 extractNum(s); // 调用extractNum函数,选出数字 cout<<s<<endl; // 输出选出的数字 return 0; } // 函数extractNum:选出str指向的字符串中的数字,并写回str // 参数:str-指向字符串 void extractNum(char * str) { // 请在此添加代码,实现函数extractNum /********** Begin *********/ /********** End **********/
时间: 2023-07-18 11:53:11 浏览: 72
void extractNum(char * str) {
bool negativeFlag = false; // 标记是否有负号
int index = 0; // 新字符串的下标
bool numFlag = false; // 标记是否已经有数字
for (int i = 0; i < strlen(str); i++) {
if (str[i] == '-') { // 如果是负号
if (!numFlag) { // 如果还没有数字
negativeFlag = true; // 记录负号
numFlag = true; // 标记已经有数字了
str[index++] = '-'; // 将负号放入新字符串
}
} else if (isdigit(str[i])) { // 如果是数字
numFlag = true; // 标记已经有数字了
str[index++] = str[i]; // 将数字放入新字符串
}
}
str[index] = '\0'; // 新字符串结尾加上'\0'
if (!numFlag) { // 如果没有数字
str[0] = '\0'; // 新字符串为空
}
}
相关问题
本关任务:在一串隐藏着一个或多个数值的字符中,选出字符串中的所有数字字符,并将选出的数字字符重新组成新字符串。如果在第一个数字字符之前有负号,则保留该负号,有多个负号时只保留一个。 例如:输入的字符串为“a-1-2-4sd5 s6”,抽取数字后得到的新字符串为“-12456”。用C++语言
可以使用C++中的字符串和循环结构来实现此任务,示例代码如下:
```c++
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "a-1-2-4sd5 s6";
string res = "";
bool has_negative = false; // 标记是否已有负号
for (int i = 0; i < s.size(); i++) {
if (s[i] >= '0' && s[i] <= '9') { // 如果当前字符是数字字符,则加入结果字符串
res += s[i];
} else if (s[i] == '-') { // 如果当前字符是负号
if (!has_negative && i == 0) { // 如果是第一个字符,则加入结果字符串
res += s[i];
has_negative = true;
} else if (s[i - 1] == '-') { // 如果前一个字符也是负号,则不加入结果字符串
continue;
} else { // 否则加入结果字符串
res += s[i];
has_negative = true;
}
}
}
cout << res << endl;
return 0;
}
```
输出结果为:-12456
本关任务:从键盘输入一个字符串,用选择法对该= 符串中的字符由 小到大进行排序,并输出结果。c语言
实现代码如下:
```c
#include <stdio.h>
#include <string.h>
void selectSort(char str[], int len) {
for (int i = 0; i < len - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < len; j++) {
if (str[j] < str[minIndex]) {
minIndex = j;
}
}
if (minIndex != i) {
char temp = str[i];
str[i] = str[minIndex];
str[minIndex] = temp;
}
}
}
int main() {
char str[100];
printf("请输入一个字符串:");
scanf("%s", str);
int len = strlen(str);
selectSort(str, len);
printf("排序后的字符串为:%s\n", str);
return 0;
}
```
代码思路:
1. 首先从键盘输入一个字符串,使用 `scanf` 函数读入字符串。
2. 然后计算字符串的长度,使用 `strlen` 函数计算字符串长度。
3. 使用选择排序算法对字符串进行排序,具体过程如下:
- 外层循环控制每次选择的最小值的位置,循环变量 i 初始值为 0,结束条件为 len - 1,即只需要选出 len - 1 个最小值。
- 内层循环从 i + 1 开始遍历字符串,找出最小值所在的位置,循环变量 j 初始值为 i + 1,结束条件为 len,即遍历字符串中剩余的所有字符。
- 如果找到了一个比 str[i] 小的字符,就将最小值位置更新为 j。
- 如果最小值位置不是 i,说明找到了一个比 str[i] 小的字符,将 str[i] 和 str[minIndex] 交换位置。
4. 最后输出排序后的字符串。
运行示例:
```
请输入一个字符串:dcba
排序后的字符串为:abcd
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)