c语言 编写函数:字符串的大小写转换

题目描述
将输入的一个字符串s中的大写字母转换为小写字母,小写字母转换为大写字母,其他字符不变。
-----------------------------------------------------------------------------
编写一个函数str_trans():
原型:void str_trans(char s[]);
功能:把串s中的大写字母转换为小写字母,小写字母转换为大写字母,其他字符不变。
函数的调用格式见“Append Code”。

输入
输入为一个串s。输入最少为一个字符,最多不会超过100个字符。

输出
输出为转换后的串。

样例输入
abc123ABC
样例输出
ABC123abc

#include <stdio.h>
int main()
{ char s[101];
gets(s);
str_trans(s);
puts(s);
return 0;
}

#include <stdio.h>
void str_trans(char c[])
{
     for(int i=0;c[i];i++)
     {
         if(c[i]<='z' && c[i]>='a')
         {
           c[i]=(c[i]-'a')+'A';
         }else if(c[i]>='A'&&c[i]<='Z')
         {
            c[i]=(c[i]-'A')+'a';
         }
     }
}
int main()
{ char s[101];
  gets(s);
  str_trans(s);
  puts(s);
  scanf("%s",s);
  return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-12-18
#include <stdio.h>

void str_trans(char s[]);

int main()
{ char s[101];
gets(s);
str_trans(s);
puts(s);
return 0;
}

void str_trans(char s[])
{
for(int i=0;i!=101;i++)
{

if(s[i] >= 65 && s[i] <= 90)
{
s[i] += 32;
}
else if (s[i] >= 97 && s[i] <= 122)
{
s[i] -= 32;
}
}
}
第2个回答  2014-12-18
void str_trans(char s[]){
int len,i;
len=strlen(s);
for(i=0;i<len,i++){
if('a'<=s[i]&&s[i]<='z')
s[i]-=32;
if('A'<=s[i]&&s[i]<='Z')
s[i]+=32;
}
}
第3个回答  2014-12-18
#include
void main()
{
char s[100];
int i=0;
gets(a);
while(a[i])
{
if(s[i] >= 'A' && a[i] <= 'Z')
s[i] += 32;
else if(s[i] >= 'a' && s[i] <= 'z')
s[i] -= 32;
i++;
}
puts(s);
}

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网