字符串分类统计
题目题目描述
输入一行字符,分别统计出其中英文字母、数字、空格和其他字符的个数。
输入
一行字符
输出
统计值
样例输入
aklsjflj123 sadf918u324 asdf91u32oasdf/.';123
样例输出
23 16 2 4
提示
此类题目为编程入门基本语法巩固练习,为单组测试数据
我的 代码 运行后的结果
22 17 2 4
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
struct a{
struct a *previous;
char b;
struct a *next;
};
int main(){
char temp=getchar();
struct a *c=malloc(sizeof(struct a));
if(c!=NULL)c->previous=NULL;
struct a *swap=NULL;
do{
c->next=malloc(sizeof(struct a));
if(c->next!=NULL){
swap=c;
c=c->next;
c->previous=swap;
c->b=temp;
}else{
printf("malloc failed!\n");
return 1;
}
temp=getchar();
if(temp!=10){
c->b=temp;
}else{
break;
}
}while(1);
c->next=NULL;
int letter=0,space=0,digit=0,specialchar=0;
do{
if(isalpha(c->b)){
letter++;
}else if(isspace(c->b)){
space++;
}else if(isdigit(c->b)){
digit++;
}else{
specialchar++;
}
c=c->previous;
free(c->next);
c->next=NULL;
}while(c->previous!=NULL);
printf("%d %d %d %d\n",letter,digit,space,specialchar);
return 0;
}
谢谢
#include <stdio.h>
main(void)
{
char x , s ;
int b , c , d , m , o ;
for(b = 0 , c = 0 , d = 0 , m = 0 , o = 0 ; (x = getchar()) != '\n'&& m < 255 ; s = x , s = '\0') {
if((x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z')) c ++ ;
else if(x >= '0' && x <= '9') d ++ ;
else if(x == ' ') b ++ ;
else o ++ ;
}
printf("%d %d %d %d\n" , c , d , b , o)
}
编译运行实况:
C:\Bin>g++ -o x x.c
C:\Bin>x
aklsjflj123 sadf918u324 asdf91u32oasdf/.';123
23 16 2 4
C:\Bin> 为啥要弄这么复杂,还用链表?
#include <stdio.h>
#include <ctype.h>
int main()
{
printf("Please input one line:\n");
char ch = getchar();
int letters = 0;
int digits = 0;
int spaces = 0;
int others = 0;
while (ch != '\n')
{
if (isalpha(ch))
{
letters++;
}
else if (isspace(ch))
{
spaces++;
}
else if (isdigit(ch))
{
digits++;
}
else
{
others++;
}
ch = getchar();
}
printf("letters: %d, digits: %d, spaces: %d, others: %d", letters, digits, spaces, others);
return 0;
} #include<stdio.h>
void fun1(char* s ){
int a,b,c,d;
a=b=c=d=0;
for(int i=0;s!='\0';i++)
{
if((s>='a'&&s<='z')||(s>='A'&&s<='Z'))a++;
else if(s>='0'&&s<='9') b++;
else if(s==' ')c++;
else d++;
}
printf("%d %d %d %d",a,b,c,d);
}
int main()
{
char s;
scanf("%[^\n]s",&s);
fun1(s);
return 0;
} #include<stdio.h>
#include<stdlib.h>
#define MAX_VOLUMN 255
int main()
{
char buffer = "aklsjflj123 sadf918u324 asdf91u32oasdf/.';123";
char *p = buffer;
int ret = {0};
while(*p != '\0')
{
if((*p >= 65 && *p <=90)||(*p >=97 && *p <=122))
ret += 1;
else if (*p >= 48 && *p <= 57)
ret += 1;
else if(*p == 32)
ret += 1;
else
ret += 1;
p++;
}
int i;
for(i = 0; i < 4; i++)
{
printf("%d ", ret);
}
printf("\n");
return 0;
}
页:
[1]