对输入的字符串,要求实现,从大到小排序,反序,统计其中任意字符的个数(包括空格),大小写转换~
----------------解决方案--------------------------------------------------------
排序用数组来做,统计字数可以用
while()
{}
实现
----------------解决方案--------------------------------------------------------
#include <stdlib.h>
#include <stdio.h>
main()
{
char str[20];
int i,j,p,temp;
scanf("%s"str);
for(i=0;i<n-1;i++)
{
p=i;
for(j=i+1;i<n;j++)
if(str[j]<str[p])p=j;
if(p!=i)
{
temp=str[p];
str[p]=sr[i];
str[i]=temp;
}
}
printf(“%s”str);
for(i=0;i<n-1;i++)
{
p=i;
for(j=i+1;i<n;j++)
if(str[j]>str[p])p=j;
if(p!=i)
{
temp=str[p];
str[p]=sr[i];
str[i]=temp;
}
}
printf(“%s”str);
if (str[i]>=’a’&&str[i]<=’z’)
str[i]=str[i]-32;
if(str[i]>=’A’&&str[I]<=’Z’)
str[i]= str[i]+32;
else str[i]=str[i];
printf(“%s”str);
printf("%d",strlen(str));
exit(0);
}
这是我写的,实在是不知道行不行啊
----------------解决方案--------------------------------------------------------
#include<stdio.h>
#include <string.h>
main()
{
char a[100],t;
int i,up,j,low,kg,num;
up=low=kg=num=0;
gets(a);
for(i=0;i<strlen(a);i++)
for(j=0;j<strlen(a)-i;j++)
if(a[j]<a[j+1])
{ t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
puts(a);
for(i=0;i<strlen(a);i++)
{
if(a[i]>='A'&&a[i]<='Z')
{
a[i]=a[i]+32;
up++;
}
else if(a[i]>='a'&&a[i]<='z')
{
a[i]=a[i]-32;
low++;
}
else if(a[i]==' ')
kg++;
else if(a[i]>='0'&&a[i]<='9')
num++;
}
for(i=0;i<strlen(a);i++)
{
printf("%c",a[i]);
}
printf("\n");
printf("%d,%d,%d,%d\n",up,low,kg,num);
getch();
}
----------------解决方案--------------------------------------------------------
大小写转换用位操作:
ch|=32; //toupper(ch);
ch&=223;//tolower(ch);
反序用strrev(str);
排序用qsort();
----------------解决方案--------------------------------------------------------
呵呵,初学还是自己写的好.
----------------解决方案--------------------------------------------------------
好啊!
----------------解决方案--------------------------------------------------------