當前位置:首頁 » 編程語言 » c語言統計字元有次數
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言統計字元有次數

發布時間: 2023-03-24 21:25:04

① 用c語言編寫一個程序,輸入一個字元串,統計其中各個字元出現的次數

源程序代碼如下:

#include "pch.h"

#define _CRT_SECURE_NO_WARNINGS//VS環境下需要,VC不需要

#include<stdio.h>

int main()

{

char c = 0;//定義輸入字元變數

int num_count = 0;//數字個數

int bigalp_count = 0;//大寫字母個數

int littlealp_count = 0;//小寫字母個數

int emp_count = 0;//空格個數

int els_count = 0;//其他字元個數

while((c = getchar()) != ' ')//連續輸入字元直到輸入回車結束

{

if((c >= '0')&&(c <= '9'))//判斷是否是數字

{

num_count ++ ;

}

else if ((c >= 'a') && (c <= 'z'))//判斷是否是小寫字母

{

littlealp_count++;

}

else if ((c >= 'A') && (c <= 'Z'))//判斷是否是大寫字母

{

bigalp_count++;

}

else if(c == ' ')//判斷是否是空格

{

emp_count ++;

}

else //判斷是否其他字元

{

els_count ++;

}

}

//輸出個數統計值

printf("數字個數:%d 小寫字母個數:%d 大寫字母個數:%d ",num_count, littlealp_count, bigalp_count);

printf("空格個數:%d 其他字元個數:%d ", emp_count, els_count);

return 0;

}

程序運行結果如下:



(1)c語言統計字元有次數擴展閱讀:

其他實現方法:

#include <stdio.h>

#include <ctype.h> //對空白字元的判斷,調用了isspace()函數,所以要調用頭文件

int main()

{

char str[20]; //這塊對輸入有所限制了

int num_count=0;

int space_count=0;

int other_count=0;

char *p=str;

gets(str); //接收字元串

while(*p)

{

if(*p>='0'&&*p<='9')

{

num_count++;

}

else if(isspace(*p)) //用isspace函數來判斷是不是空白字元

{

space_count++;

}

else

{

other_count++;

}

p++;

}

printf("num_count=%d ",num_count);

printf("space_count=%d ",space_count);

printf("other_count=%d ",other_count);

return 0;

}

② 用C語言編寫一個程序,輸入一個字元串,統計其中各個字元出現的次數

//輸入一行字元,分別統計出其中字母、空格、數字和其他字元的個數。
#include<stdio.h>
intmain(void)
{
charch;
inta=0,b=0,c=0,d=0;
while((ch=getchar())!=' ')
{
if(ch>='A'&&ch<='Z'||ch>='a'&&ch<='z')
a++;
elseif(ch>='0'&&ch<='9')
b++;
elseif(ch=='')
c++;
else
d++;
}
printf("字母=%d 數字=%d 空格=%d 其他字元=%d ",a,b,c,d);
return0;
}

③ C語言 統計字元出現次數

#include <stdio.h>

int main()

{ int i,n=0;

char c,s[100];

gets(s);

c=getchar();

for(i=0; s[i]; i++)

if(c==s[i])n++;

printf("%d",n);

return 0;

}

④ c語言 統計字元個數

要統計英文字母,空格,數字和其他字元的個數,代碼如下:
#include<stdio.h>

#include<stdlib.h>
int main()
{
char c;
int letters=0;
int space=0;
int digit=0;
int other=0;
printf("請輸入一行字元:>");
while((c=getchar())!='\n')
{
if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))
{
letters++;
}
else if(''==c)
{
space++;
}
else if(c>='0'&&c<='9')
{
digit++;
}
else
{
other++;
}
}
printf("字母的個數:>%d\n空格的個數:>%d\
\n數字的個數:>%d\n其他字元的個數:>%d\n",\
letters,space,digit,other);
system("pause");
return 0;
}

⑤ 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&lt;stdio.h&gt;

#include&lt;stdlib.h&gt;

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++)//循環整個串

{

if(ptr2-ptr&lt;len)//如果一開始子串就大於src,則退出

break;

for(prev=ptr;*prev==*p;prev++,p++)//尋找第一個相等的位置,然後從此位置開始匹配子串

{

if(*(p+1)==0||*(p+1)==10)//如果已經到了子串的末尾

{

n++;//自增

p=s;//重新指向子串

break;//退出

}

}

}

return n;

}

int main(void)

{

char a[81],b[81];//定義兩個字元數組

printf("輸入原字元串:");

fgets(a,81,stdin);//輸入

printf("輸入子字元串:");

fgets(b,81,stdin);

printf("找到:%d ",findsub(a,b));

system("pause");

return 0;

}

(6)c語言統計字元有次數擴展閱讀:

①printf()函數是格式化輸出函數,一般用於向標准輸出設備按規定格式輸出信息。

格式輸出,它是c語言中產生格式化輸出的函數(在stdio.h中定義)。用於向終端(顯示器、控制台等)輸出字元。格式控制由要輸出的文字和數據格式說明組成。

要輸出的文字除了可以使用字母、數字、空格和一些數字元號以外,還可以使用一些轉義字元表示特殊的含義。

簡單點理解就是,在一段程序中你正確使用該函數,可以在該程序運行的時候輸出你所需要的內容。

②printf函數是一個標准庫函數,它的函數原型在頭文件「stdio.h」中。但作為一個特例,不要求在使用printf函數之前必須包含stdio.h文件。

printf()函數的調用格式為:printf("&lt;格式化字元串&gt;",&lt;參量表&gt;)。

其中格式化字元串用於指定輸出格式。格式控制串可由格式字元串和非格式字元串兩種組成。

⑦ C語言編程:輸入一串字母,統計每個字母出現的次數

C語言程序如下:

#include<stdio.h>

int main()

{

char a[100];

char b[24];

int s[100] = { 0 };//用於存儲字元的個數

gets(a);//輸入字元

//開始比較

for (int x = 0; x < 24; x++)

{

int c = 0;//記錄每個字元個數

b[x] = x + 97;//為了讓b[0]是a,b[1]是b依次類推

for (int i = 0; i < 100; i++)

{

if (b[x] == a[i])

{

++c;

s[x] = c;

}

}

if (s[x]>=1)//只輸出輸入中有的字母 的個數

{

printf("%c %d ", b[x], s[x]);

}

}

getchar();

return 0;

}

(7)c語言統計字元有次數擴展閱讀:

程序思路:
分為三部分 首先輸入字元串 ,其次設定一個字元數組英文小寫字母24, 同時設一個int數組 記錄個數, 以及一個int c 為了給int數組賦值。最後在輸入的時候進行判斷,如果字母的值 大於等於1才輸出。