本题目要求读入一行字符串,去掉首,尾的多余空格后输出。
输入格式:
一个字符串,长度不限,以换行表示结束,但程序要求读取个数不超过30个。
输出格式:
在一对[]中先输出读入的字符串,再在下一行的一对[]中输出去掉首尾空格后的字符串。
输入样例:
湖北 襄阳
输出样例:
[ 湖北 襄阳 ]
[湖北 襄阳]
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char str_1[1000]= {0},str_2[1000]= {0},str_3[1000]= {0};
void trim(char * str) {
int index, i;
/*
* Trim leading white spaces
*/
index = 0;
while(str[index] == ' ' || str[index] == '\t' || str[index] == '\n') {
index++;
}
/* Shift all trailing characters to its left */
i = 0;
while(str[i + index] != '\0') {
str[i] = str[i + index];
i++;
}
str[i] = '\0'; // Terminate string with NULL
/*
* Trim trailing white spaces
*/
i = 0;
index = -1;
while(str[i] != '\0') {
if(str[i] != ' ' && str[i] != '\t' && str[i] != '\n') {
index = i;
}
i++;
}
/* Mark the next character to last non white space character as NULL */
str[index + 1] = '\0';
}
int main() {
scanf("%c",&str_1[0]);
int p=0;
while(str_1[p]!='\n') {
p++;
scanf("%c",&str_1[p]);
}
str_2[0]='[';
for(int i=1; i<p; i++) {
str_2[i]=str_1[i-1];
}
str_2[p+1]=']';
for(int j=0; j<=p+1; j++) {
printf("%c",str_2[j]);
}
printf("\n");
trim(str_1);
printf("[");
printf("%s",str_1);
printf("]");
return 0;
}