输入字符串,统计字符串中字符个数,大写字母个数和小写字母个数,并将...
发布网友
发布时间:2024-03-09 02:52
我来回答
共3个回答
热心网友
时间:2024-03-12 00:33
C语言源码及注释如下(运行结果见附图):
#include <stdio.h> /* standard input & output support */
int main (int argc, const char * argv[])
{
char input[1024] = {0};
printf("Please input a string:");
scanf("%s", input);
printf("string(old) = %s\n", input);
int tcount = 0; /* 总字符数 */
int lcount = 0; /* 小写字母数 */
int ucount = 0; /* 大写字符数 */
char *p = input;
while (*p != '\0')
{
if (*p >= 'a' && *p <= 'z')
{
lcount++;
*p &= 0xDF; /* 小写字母变大写字母 */
}
else if (*p >= 'A' && *p <= 'Z')
{
ucount++;
}
tcount++;
p++;
}
printf("It contains %d character(s), %d uppercase letter(s) and %d lowercase letter(s).\n",
tcount, ucount, lcount);
printf("string(new) = %s\n", input);
return 0;
}
热心网友
时间:2024-03-12 00:26
#include <stdio.h>
#define N 200
void count(char *);
int main()
{
char *ch,chr;
ch=malloc(N+1);
printf("请输入输入一行字符:\n");
gets(ch);
count(ch);
getchar();
}
void count(char *ch)
{
char *temp=ch;
int i, upper=0,lower=0,digit=0;
while(*ch != '\0')
{
if(*ch>='A' && *ch<='Z' )
upper++;
else if(*ch>='a' && *ch<='z')
{*ch=*ch+'A'-'a';
lower++;}
digit++;
ch++;
}
printf("该字符串共有%d个字符,其中大写字母有%d个,小写字母有%d个。\n小写转换为大写后,字符串变成了:%s\n",digit,upper,lower,temp);
}
热心网友
时间:2024-03-12 00:32
class Program
{
public static string newstring="";
public static string mymethod(string s)
{
int num1 = 0;
int num2 = 0;
int num3 = 0;
for (int i = 0; i < s.Length; i++)
{
char C = s[i];
int ASC = C;
if (ASC >= 65 && ASC <= 90)
{
num1 += 1;
newstring+=s[i];
}
else if (ASC >= 97 && ASC <= 122)
{
num2 += 1;
newstring+= s[i].ToString().ToUpper();
}
else if (ASC >= 48 && ASC <= 57)
{
num3 += 1;
newstring += s[i];
}
}
string result = "字符串中大写字母数为" + num1 + "个,小写字母数为" + num2 + "个,数字为" + num3 + "个";
return result;
}
static void Main(string[] args)
{
Console.WriteLine("请输入一个字符串");
string s = Console.ReadLine();
Console.WriteLine(mymethod(s));
Console.WriteLine("将小写字母全部转换为大写字母结果为:");
Console.WriteLine(newstring);
Console.ReadKey();
}
}