标题:如何去掉字符串里不必要的空格,包括前面和后面的。
取消只看楼主
winglesswu
Rank: 1
等 级:新手上路
帖 子:92
专家分:0
注 册:2013-1-28
结帖率:71.88%
已结贴  问题点数:3 回复次数:1 
如何去掉字符串里不必要的空格,包括前面和后面的。
要求写一个函数,输出一个新的字符串和字符个数(不包括null),去掉原字符串里不必要的空格,包括前面和后面的,单词和单词之间只能有一个空格。比如:
原字符串为char str[ ] = "   This is    a     test.      "
输出后为:
'This is a test.' contains 15 characters.
下面是小弟写的,但是运行不了,请大侠支招,谢谢。
#include<stdio.h>
#include<string.h>
#include <ctype.h>
int main()
{
   char str[100];
   int i;
 
   printf("Enter some text:\n");
   scanf("%s", str);
   i = compress(str);
   printf ("'%s' contains %d characters.\n", str, i);
}
int compress ( char text[100] )
{
int x, i, j=0, d=0;
char blank[100];
if (isupper(text[i])||islower(text[i]))//确定首个字母的位置
{
for (i<100; i++)
{
      if (!(text[i] == ' ' && text[i+1] == ' '))
      {
        blank[j] = text[i];
        j++;
      }
   }
 }
 
   blank[j] = '\0';
 
   strcpy (text, blank);
   x=strlen(text);
   return x-1;//请问如何返回新的字符串?
   }
搜索更多相关主题的帖子: test include 字符串 Enter 
2013-04-10 08:21
winglesswu
Rank: 1
等 级:新手上路
帖 子:92
专家分:0
注 册:2013-1-28
得分:0 
谢谢大家的指点。
2013-04-10 23:02



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-403562-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.185012 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved