急!急!!急!!!输入一个字符串,拴除它前后的空格符

来源:百度知道 编辑:UC知道 时间:2024/05/15 20:12:52
RT!要能运行的,C或C++都行
例如:
输入abcde(假如它前后都有空格符)
涮掉,然后输出abcde(字符串中间的空格符不用管)

#include <stdio.h>
#include <string.h>

void main()
{
int i, j, l;
char str[100];

/* 最多输入100个字符,遇到换行符终止 */
scanf("%100[^\n]", str);
l = strlen(str);
printf("%d\n", l);
i = 0;
while (str[i++] == ' ') ;

--i;
j = i;

while (str[j] != '\0')
{
str[j - i] = str[j];
j++;
}

/* 重新设置字串结束符 */
str[l-i] = '\0';
printf("%s\n", str);
}