㈠ 用c語言編寫輸入一字元串,統計字元串中各個字母出現的次數(區分大小寫)
//輸入一行字元,分別統計出其中字母、空格、數字和其他字元的個數。
#include
int main(void)
{
char ch;
int a=0,b=0,c=0,d=0;
while((ch=getchar())!='\n')
{
if(ch>='a'&&ch<='z'||ch>='a'&&ch<='z')
a++;
else if(ch>='0'&&ch<='9')
b++;
else if(ch==' ')
c++;
else
d++;
}
printf("字母=%d\n數字=%d\n空格=%d\n其他字元=%d\n",a,b,c,d);
return 0;
}
㈡ c語言統計每個字母出現次數
#include<stdio.h>
void
main()
{
char
ch[10]="abcDEFghiJ";
int
i=0,x=0,y=0;
printf("原數據為:");
for(;i<10;i++)
{
if(ch[i]>='a'&&ch[i]<='z')
x++;
else
if(ch[i]>='A'&&ch[i]<='Z')
{
y++;
}
printf("%c",ch[i]);
}
printf("\n其中小寫字母數為%d\n",x);
printf("其中大寫字母數為%d\n",y);
}
㈢ 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語言使用指針,編寫程序,從鍵盤r輸入一個字元串,並統計各字母出現的次數(不區分大小寫)。
摘要 是這樣的#include
㈤ C語言 編寫程序,從鍵盤輸入若干個英文字母,並統計各字母出現的次數
#include
#include
#define
max
100
int
main()
{
char
str[max];
//
輸入的字元串,最大長度是max-1,因為有一個字元串結束符
int
i
=
0,
count[52]
=
{
0
};
//
count
數組用來存儲各個字母出現的次數
scanf("%s",
str);
while(str[i]
!=
'\0')
{
if
(str[i]
>=
'a'
&&
str[i]
<=
'z')
{
//
統計小寫字母
count[str[i]
-
97
+
26]++;
}
if
(str[i]
>=
'a'
&&
str[i]
<=
'z')
{
//
統計大寫字母
count[str[i]
-
65]++;
}
i++;
}
for
(i
=
0;
i
<
26;
i++)
{
//
輸出大寫字母信息
if
(count[i]
!=
0)
printf("%c\t%d\n",
i
+
65,
count[i]);
//
只輸出不為零的數據
}
for
(i
=
26;
i
<
52;
i++)
{
//
輸出小寫字母信息
if
(count[i]
!=
0)
printf("%c\t%d\n",
i
+
97
-
26,
count[i]);
}
return
0;
}
㈥ C語言編程:輸入一串英文字母,統計每個字母(不區分大小寫)出現的次數
#include<iostream>
#include<string>
using namespace std;
int main()
{
char str[50];
int n=0;
char *p;
p=str;
cout<<"請輸入字元串:"<<endl;
cin>>str;
for(int i=0;*p!='