#include"stdio.h"
int main()
{int i,j,k; char s[]="i am a teacher.";
for(i=0,j=0,k=1;s[i];i++)
if(s[i]!=' '||k)
{
s[j++]=s[i];
k=s[i]!=' ';
}
s[j]='\0';
puts(s);
for(i=0;s[i];i++)
if(s[i]>='a'&&s[i]<='z')s[i]-=32;
puts(s);
system("pause");
return 0;
}
dtya@zzu:~$ cat a.c
#include
#include
#include
//删掉字符串中多余的空格
void trim(char a[])
{
int n=strlen(a);
int i;
for(i=1;a[i]!='\0';i++)
{
if(a[i]==' '&& a[i-1]==' ')
{
int j;
for(j=i+1;a[j]!='\0';j++)
{
a[j-1]=a[j];
}
a[j-1]='\0';
}
}
}
//大写转小写
void lower(char a[])
{
int n=strlen(a);
int i;
for(i=0;i{
if(a[i]>='A' && a[i]<='Z')
a[i]=a[i]-'A'+'a';
}
}
int main()
{
char a[128];
gets(a);
trim(a);
puts(a);
lower(a);
puts(a);
return 0;
}
dtya@zzu:~$ ./a.out
I am a TeacheR.
I am a TeacheR.
i am a teacher.
什么时候要?