C++:删除字符串中的所有空格操作

C++代码:

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
using namespace std;

string del_space(string *str) {
    string::iterator it = (*str).begin();
    while ((it = find(it, (*str).end(), ' ')) != (*str).end())
    {
        (*str).erase(it);
        if (it == (*str).end())
            break;
    }
    return *str;
}

int main() {
    string str = "   dfsdfasdf f  sdfas";
    del_space(&str);
    cout << str;
    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在C语言,我们可以使用循环和条件判断语句来删除字符串空格。具体步骤如下: 1. 首先,定义一个字符数组变量来存储字符串,例如`char str[] = "Hello World";`,其包含了多个空格。 2. 创建一个新的字符数组变量来存储删除空格字符串,例如`char newStr[sizeof(str)];`。 3. 使用循环遍历字符串的每一个字符,并判断是否为空格。 4. 如果不是空格,则将该字符添加到新的字符数组,可以使用索引变量来记录新的字符数组的位置。 5. 最后,在新的字符数组添加一个空字符`\0`来表示字符串的结束。 6. 最终得到的新的字符数组就是删除空格字符串。 代码示例: ```c #include <stdio.h> int main() { char str[] = "Hello World"; char newStr[sizeof(str)]; int newIndex = 0; // 新字符数组的索引 for (int i = 0; str[i] != '\0'; i++) { if (str[i] != ' ') { newStr[newIndex] = str[i]; newIndex++; } } newStr[newIndex] = '\0'; // 添加字符串结束符 printf("删除空格后的字符串为:%s\n", newStr); return 0; } ``` 运行以上代码,输出结果为:`删除空格后的字符串为:HelloWorld`,可以看到空格已经成功删除了。 ### 回答2: 在C语言删除字符串空格可以通过遍历字符串的每个字符,将非空格字符复制到一个新的字符串来实现。具体步骤如下: 1. 定义一个新的字符数组(字符串),用于存储删除空格后的字符串。 ```c char newStr[100]; ``` 这里假设新字符串长度不会超过100个字符。 2. 使用循环遍历原始字符串的每个字符。 ```c int i, j; for (i = 0, j = 0; str[i] != '\0'; i++) { // 如果当前字符不是空格,则将其复制到新字符串 if (str[i] != ' ') { newStr[j] = str[i]; j++; } } ``` 3. 添加字符串结束标志'\0'到新字符串的末尾。 ```c newStr[j] = '\0'; ``` 4. 删除空格后的字符串已经存储在新字符串,可以进行后续操作或打印输出。 ```c printf("删除空格后的字符串为:%s\n", newStr); ``` 请注意,在删除空格后,新字符串的长度可能会小于原始字符串的长度。 以上就是通过遍历字符数组并复制非空格字符到新的字符数组删除字符串空格的方法。 ### 回答3: 在C语言删除字符串空格可以通过以下几种方法实现: 1. 使用循环遍历字符串,将空格字符替换为其他字符或者直接删除。代码示例: ```c #include <stdio.h> #include <string.h> void deleteSpace(char* str) { int len = strlen(str); int i, j; for (i = 0, j = 0; i < len; i++) { if (str[i] != ' ') { str[j++] = str[i]; } } str[j] = '\0'; } int main() { char str[] = "Hello World!"; deleteSpace(str); printf("%s\n", str); // 输出结果为"HelloWorld!" return 0; } ``` 2. 使用库函数strtok将字符串拆分为单词,然后再将单词重新拼接为字符串。代码示例: ```c #include <stdio.h> #include <string.h> void deleteSpace(char* str) { char* token = strtok(str, " "); while (token != NULL) { strcat(str, token); token = strtok(NULL, " "); } } int main() { char str[100] = "Hello World!"; deleteSpace(str); printf("%s\n", str); // 输出结果为"HelloWorld!" return 0; } ``` 3. 使用递归函数删除字符串空格。代码示例: ```c #include <stdio.h> #include <string.h> void deleteSpace(char* str, int index) { if (str[index] == '\0') return; if (str[index] != ' ') { deleteSpace(str, index + 1); return; } memmove(&str[index], &str[index + 1], strlen(str) - index); deleteSpace(str, index); } int main() { char str[] = "Hello World!"; deleteSpace(str, 0); printf("%s\n", str); // 输出结果为"HelloWorld!" return 0; } ``` 以上是三种常见的在C语言删除字符串空格的方法,你可以根据具体需求选择其一种进行实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值