计数并去除字符数组里的空格
#include <iostream>using namespace std;class Solution {
public:int del_space(char* s, int n){int count = 0;if (s == nullptr || n == 0)return count;// 用tmp指向s,由于删除操作后的字符串不长于原有字符串s,// 可以一边遍历一边直接对s进行修改char* tmp = s;for (int i = 0; i < n; i++){// 只有不为空格时,tmp指针往前一步if (s[i] != ' '){*tmp++ = s[i];}elsecount++;}return count;}};int main()
{Solution solution;char s[] = "aaaa dd";int res = solution.del_space(s, strlen(s));cout << res << endl;return 0;
}