去除字符串中多余的空格 C语言实现
2012-09-26 22:06
781 查看
比如“hello world hey baby”
变成“hello world hey baby”
思想是设置两个指针,前面的(front)一直往前走直到字符串结尾,后面的(last)复制front当前指向的字符,
当遇到多个空格时并不复制,而是等到front指向非空格字符时在往前走。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void omitSpace(char *str){
char *front = str;
char *last = str;
if(str == NULL) return;
while((*front) == ' ') {++front;} //omit space in the beginning
while((*front) != '\0'){
if((*front) == ' '){
*last = ' ';
while((*front) == ' '){
++front;
}
}else{
*last = *front; //can also add one if condition to avoid unnecessary assignment: if(last != front){*last = *front;}
++front;
}
++last; //front has pointed to the next char, so don't ++front;
}
*last = '\0';
}
int main(){
char *cases[] = {"hello world!!!", " hello world...", " Hello world???", "he llo wor ld ... "};
char **str;
int strIndex = 0;
int letterIndex = 0;
int casesNum = sizeof(cases) / sizeof(char*); //don't forget to be divided by sizeof(char*).
str = (char**)malloc(sizeof(cases));
for(strIndex = 0; strIndex < casesNum; ++strIndex){
str[strIndex] = (char*)malloc(1 + strlen(cases[strIndex]));
for (letterIndex = 0; letterIndex < strlen(cases[strIndex]); ++letterIndex)
{
str[strIndex][letterIndex] = cases[strIndex][letterIndex];
}
str[strIndex][letterIndex] = '\0';
}
for(strIndex = 0; strIndex < casesNum; ++strIndex){
printf("%s\n", str[strIndex]);
omitSpace(str[strIndex]);
printf("%s\n", str[strIndex]);
}
for(strIndex = 0; strIndex < casesNum; ++strIndex){
free(str[strIndex]);
}
free(str);
return 0;
}
变成“hello world hey baby”
思想是设置两个指针,前面的(front)一直往前走直到字符串结尾,后面的(last)复制front当前指向的字符,
当遇到多个空格时并不复制,而是等到front指向非空格字符时在往前走。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void omitSpace(char *str){
char *front = str;
char *last = str;
if(str == NULL) return;
while((*front) == ' ') {++front;} //omit space in the beginning
while((*front) != '\0'){
if((*front) == ' '){
*last = ' ';
while((*front) == ' '){
++front;
}
}else{
*last = *front; //can also add one if condition to avoid unnecessary assignment: if(last != front){*last = *front;}
++front;
}
++last; //front has pointed to the next char, so don't ++front;
}
*last = '\0';
}
int main(){
char *cases[] = {"hello world!!!", " hello world...", " Hello world???", "he llo wor ld ... "};
char **str;
int strIndex = 0;
int letterIndex = 0;
int casesNum = sizeof(cases) / sizeof(char*); //don't forget to be divided by sizeof(char*).
str = (char**)malloc(sizeof(cases));
for(strIndex = 0; strIndex < casesNum; ++strIndex){
str[strIndex] = (char*)malloc(1 + strlen(cases[strIndex]));
for (letterIndex = 0; letterIndex < strlen(cases[strIndex]); ++letterIndex)
{
str[strIndex][letterIndex] = cases[strIndex][letterIndex];
}
str[strIndex][letterIndex] = '\0';
}
for(strIndex = 0; strIndex < casesNum; ++strIndex){
printf("%s\n", str[strIndex]);
omitSpace(str[strIndex]);
printf("%s\n", str[strIndex]);
}
for(strIndex = 0; strIndex < casesNum; ++strIndex){
free(str[strIndex]);
}
free(str);
return 0;
}
相关文章推荐
- C语言实现一个去除字符串中的首尾空格,字母中间有多个空格的去除多余的,剩余一个空格
- 去除字符串中的空格 C语言实现
- C语言去除字符串首尾空格,trim()函数实现
- C语言实现去除字符串中空格
- C语言实现去除字符串中空格
- C语言实现删除字符串多余空格
- c语言实现 去除字符串两端空格及回车 v1.0
- 去除字符串末尾的空格的C语言实现
- C语言实现去除字符串中空格的简单实例
- C语言实现去除字符串中空格
- sqlServer实现去除字符串空格
- JS实现去除一个字符串中的所有标点和空格
- 去除字符串前后和中间多余的空格
- 删除字符串中多余的空白字符和空行(C语言实现)
- 去除输入字符串中的多余空格
- C语言实现一个长字符串中去除多次出现短字符串的功能
- 4.7编写一个函数,从一个字符串中去除多余的空格。
- (C语言)字符串替换空格:请实现一个函数,把字符串中的每个空格替换成“%20”
- 写一个函数,实现去除字符串空格
- 【廖雪峰 python教程 课后题 切片】利用切片操作,实现一个trim()函数,去除字符串首尾的空格,注意不要调用str的strip()方法: