c語言字元統計
『壹』 c語言字元統計
#include<stdio.h>
intmain()
{inti,a,n=0;
charc,s[101];
scanf("%c%d%*c",&c,&a);
gets(s);
if(a==0&&c>='a'&&c<='z')c-=32;
for(i=0;s[i];i++)
if(c==(a==0&&s[i]>='a'&&s[i]<='z'?s[i]-32:s[i]))n++;
printf("%d
",n);
return0;
}
『貳』 c語言:字元分類統計
#include <stdio.h>
#include <string.h>
void main()
{
char c[100];
int i,n,ch=0,blank=0,number=0,other=0;
printf("Input something:\n");
gets(c);
n=strlen(c);
for(i=0;i<n;i++)
{
if((c[i]>='a' && c[i]<='z')||(c[i]>='A' && c[i]<='Z'))ch++;
else if(c[i]==' ') blank++;
else if(c[i]>='0' && c[i]<='9') number++;
else other++;
}
printf("ch:%d,blank:%d,number:%d,other:%d\n",ch,blank,number,other);
}
輸出的是字母,空格,數字及其他,剛調通的,請參考.
『叄』 C語言中統計字元出現的次數
是有些錯誤,我幫你改了下 ,你自己看看:
# include "stdio.h"
# include "string.h"
main()
{
char a[100],b[100];
int i,n1,n2,k,num,flag;
printf("輸入原文:\n");
scanf("%s",a);
fflush(stdin); /* flush the input stream in case of bad input */
printf("輸入字元串:\n");
scanf("%s",b);
fflush(stdin); /* flush the input stream in case of bad input */
n1=strlen(a);
n2=strlen(b);
num=0;
for(i=0;i<n1-n2;i++)
{
for(k=i;k<n2+i;k++)
{
flag=1;
if(a[k]!=b[k-i])
{
flag=0;
break;
}
if((k-i)==(n2-1))
{
num++;
i+=n2;
}
}
}
printf("總共出現%d\n",num);
system("pause");
}
首先,最好是在scanf("%s",a);後面加上這句 fflush(stdin);
/* flush the input stream in case of bad input */
其次,你的思路有問題,在第二個for循環裡面
第二個if的條件應該改成(k-i)==(n2-1),即檢查到了字元數組b的結尾了,說明前面的都匹配,那麼才加1,呵呵呵,自己好好想想吧
『肆』 c語言統計字元個數
#include<stdlib.h>
#include<stdio.h>
intmain(void)
{
charinput;
intdaxie=0,xiaoxie=0,kongge=0,number=0,others=0,count=0;
while((input=getchar())!=' ')
{
if(input>='a'&&input<='z')
xiaoxie++;
elseif(input>='A'&&input<='Z')
daxie++;
elseif(input=='')
kongge++;
elseif(input>='0'&&input<='9')
number++;
else
others++;
count++;
}
printf("大寫字母個數為%d,小寫字母個數為%d,空格字元個數為%d,數字個數為%d,其他字元個數為%d,總的字元個數為%d ",daxie,xiaoxie,kongge,number,others,count);
system("pause");
return0;
}
『伍』 C語言編程:統計字元串中各字母出現的次數
#include<stdio.h>
#include<stdlib.h>
int findsub(char*src,char*s)
{
char*ptr=src,*p=s;//定義兩個指針
char*ptr2=src+strlen(src),*prev=NULL;//ptr2為src的末位置指針
int len=strlen(s),n=0;//子串的長度和計數器
for(;*ptr;ptr++)//循環整個串
n++;//自增
p=s;//重新指向子串
break;//退出
char a[81],b[81];//定義兩個字元數組
fgets(b,81,stdin);
printf("找到:%d ",findsub(a,b));
system("pause");
return 0;
}
數據類型:
字元串數據類型是建模在形式字元串的想法上的數據類型。字元串是幾乎在所有編程語言中可以實現的非常重要和有用的數據類型。在某些語言中它們可作為基本類型獲得,在另一些語言中做為復合類型獲得。多數高級語言的語法允許通常用某種方式引用起來的字元串來表示字元串數據類型的實例;這種元字元串叫做「文本」或「字元串文本」。
以上內容參考:網路-字元串
『陸』 C語言:字元串統計
int count[36]={0};
int i = 0;
char ch = 0;
char str[200] = {0};
printf("input string:")
scanf("%s",str);
for (i=0;i<strlen(str);i++)
{
ch=str[i];
if ((ch<'A')||(ch>'Z')) continue;
count[ch-'A'] ++;
}
顯示:
for(i=0;i<36;i++)
{
if (count[i]==0) continue;
printf("%c:%d ",i+'A', count[i]);
}
截圖上傳不上去,代碼也不能格式化。
『柒』 c語言統計字元串中每個字元出現的次數
#include<stdio.h>
int main()
{ int i,a[128]={0};
char ch;
while((ch=getchar())!=' ')a[ch]++;
for(i=32;i<128;i++)
if(a[i])
printf("%c: %d ",i,a[i]);
return 0;
}
『捌』 C語言 統計中文字元數
#include<stdio.h>
void
coun(char
s[])
{
int
i,m=0,n=0,p=0,d=0;
for(i=0;s[i]!='\0';i++)
{if(s[i]=='
')
p++;
else
if((s[i]>='0')&&(s[i]<='9'))
m++;
else
if((s[i]>='A')&&(s[i]<='Z')||(s[i]>='a'&&s[i]<='z'))
n++;
else
d++;
}
printf("輸入的字元串是:%s\n數字的個數:%d\n字母的個數:%d\n空格的個數:%d\n其它字元個數:%d\n",s,m,n,p,d);
}
int
main()
{
char
a[100];
gets(a);
coun(a);
system("pause");
return
0;
}