任务正在跟随。 实现函数char * stringstat(const char * s),它将计算字符数,数字,空格字符和不可打印的字符数,并输出结果字符串,形式为:characters:w digits:x spaces:y non-print: z,其中w,x,y和z是相应的量。 到目前为止,我已经收集了这些原材料。
#include <stdio.h>
#include <string.h>
using namespace std;
int main(void)
{
char str[]="something.-21";
int length = strlen(str);
printf("The ASCII value of %c is %d ",character,storeAscii);
if (storeAscii>=65 && storeAscii<=90)
{
printf("\nYou have entered a capital letter");
}
else if (storeAscii>=97 && storeAscii<=122)
{
printf("\nYou have entered a small letter");
}
else if (storeAscii>=47 && storeAscii<=57)
{
printf("\nYou have entered a digit ");
}
else if (storeAscii>=0 && storeAscii>=47
|| storeAscii>=54 && storeAscii<=64
|| storeAscii>=91 && storeAscii<=96
|| storeAscii>=123 && storeAscii<=127)
{
printf("\nYou have entered a special character");
}
return 0;
}
我知道我必须拥有&#34;因为&#34;检查字符串中的每个符号并在符号上挂起的循环添加count ++然后我可以输出数量。我真的不知道如何对字符串进行循环检查。
非常感谢timrau 与此任务相对应的最终产品是:
#include<stdio.h>
#include<string.h>
#include <cctype>
int main()
{
char str[]="This sentence is messed up. It has 32413523.";
int w = strlen(str);
int x=0,y=0,z=0;
for(char *ptr = str; *ptr!='\0';++ptr)
{
if(isdigit(*ptr)){x++;}
else if(isblank(*ptr)){y++;}
else if(isprint(*ptr)){z++;}
}
printf("In sentence \"%s\" there is:\n",str);
printf("Characters: %d\n",w);
printf("Digits: %d\n",x);
printf("Space characters: %d\n",y);
printf("Non-printable characters: %d\n",z);
return 0;
}
答案 0 :(得分:2)
#include <cctype>
for (char *ptr = str; *ptr != '\0'; ++ptr)
{
if (isupper(*ptr)) { /* upper case */ }
else if (islower(*ptr)) { /* lower case */ }
else if (isdigit(*ptr)) { /* decimal digit */ }
else { /* special */ }
}