作业帮 > 综合 > 作业

输入一个英文句子,如果两个句子间有多于一个空格,删除多余的空格,然后输出处理前后的句子长度

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/19 09:22:53
输入一个英文句子,如果两个句子间有多于一个空格,删除多余的空格,然后输出处理前后的句子长度
最好用到strlen函数
PS:初学者,不太明白,希望各位帮帮忙
多谢了!
#include
  #include
  #define MAX_LEN 128
  /**
  * 该函数的作用为当有两个连续空格出现时,
  * 去掉多余的那个空格.因为是循环执行,故当有多个
  * 连续空格出现时,也可以删到只剩一个空格.
  */
  int fun( char *input_str,char *output_str )
  {
  int i,j;
  char *p;
  strcpy( output_str,input_str );
  p = output_str;
  /* 去掉句子开头的空格 */
  while( *p = ' ' )
  for( i = 0; i < strlen( p ); i++ )
  *( p + i ) = *( p + i + 1 );
  /* 去掉句子中间多余的空格 */
  for( i = 0; i < strlen( p ); i++ )
  {
  if( *( p + i ) == ' ' )
  {
  while( *( p + i + 1 ) == ' ' )
  {
  j = i + 1;
  while( *( p + j + 1 ) != '\0' )
  {
  *( p + j ) = *( p + j + 1 );
  j++;
  }
  *( p + j ) = '\0';
  }
  else continue;
  }
  else continue;
  }
  /* 去掉句子结尾的空格 */
  while( *( p + strlen(p) - 1 ) == ' ' )
  *( p + strlen(p) - 1 ) = '\0';
  return 0;
  }
  /* this is a test */
  void main()
  {
  char input[]=" this is a input str for test!";
  char output[ MAX_LEN ];
  fun( input,output );
  /* 输出调整前后句子的值及长度 */
  printf("befor run fun,str is:[%s] \nstrlen1=%d\n",input,strlen( input) );
  printf("after run fun,str is:[%s] \nstrlen2=%d\n",output,strlen(output) );
  }