eg:"____hello____world__how__"(空格用/代替)
思想:扫描字符串,若相邻两个都是空格,则应去掉后面的空格;因此设置两个指针,一个指针pstep用于扫描原字符串,另一个指针pn用于指示新字符串;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void del_spacebar(char *str) {
char *pstep = str;
char *pn=str;
while (*pstep != '\0') {
if (*pstep == '_'&&*(pstep + 1) != '_' || *pstep != '_') {
*pn++ = *pstep;
}
pstep++;
}
*pn = '\0';
}
int main() {
char str[] = "____hello____world__how__";
del_spacebar(str);
puts(str);
}