如何在C中打印字母和单词(鼓励使用指针)
我已经用我的密码取得了这么大的进步。我的策略是让数组索引指示该索引存储的是哪个字母的出现次数。例如:如何在C中打印字母和单词(鼓励使用指针),c,string,pointers,C,String,Pointers,我已经用我的密码取得了这么大的进步。我的策略是让数组索引指示该索引存储的是哪个字母的出现次数。例如:'a'=65'b'=66'b'-'a'=1,意思是字母[1]保存'b'的出现次数。 我需要帮助打印每个字母和出现的次数,因为数组的索引表示每个字母,它们是数字。我甚至不知道如何根据单词的长度(1、2、3等字母单词)计算单词的出现次数。最困难的部分可能是按字母顺序输出所有单词。我已经完成了代码中字母的出现部分,但是我得到了这个疯狂的错误 具体说明见该照片: #包括 #包括 #包括 int main
'a'
=65
'b'
=66
'b'-'a'
=1
,意思是字母[1]
保存'b'
的出现次数。我需要帮助打印每个字母和出现的次数,因为数组的索引表示每个字母,它们是数字。我甚至不知道如何根据单词的长度(1、2、3等字母单词)计算单词的出现次数。最困难的部分可能是按字母顺序输出所有单词。我已经完成了代码中字母的出现部分,但是我得到了这个疯狂的错误 具体说明见该照片:
#包括
#包括
#包括
int main()
{
国际标准长度;
int*ptr_strLength;
字符文本流[];
int cont=1;
while(cont!=0)
{
scanf(“%s”,textStream[]);
strLength=strlen(textStream[]);
}
ptr_strLength=&strLength;
void findLetters(ptr_strLength);
)
void findLetters(int*ptr)//查找所有字母的出现处
{
整数[26];
国际律师协会[26];
字符文本[];
整数指数;
INTA=65;
int-lowA=97;
int b=0;
int c=0;
int d=0;
int e=0;
int i=0;
整数长度;
长度=*ptr;
while(textStream[i]!='\0')
{
对于(;i64&&textStream[i]<91)//存储出现的大写字母
{
textStream[i]-a=索引;
upLetters[index]=upLetters[index]+textStream[i]-textStream[i]+1;
}
if(textStream[i]>89&&textStream[i]<123)//存储出现的小写字母
{
textStream[i]-lowA=索引;
loLetters[index]=loLetters[index]+textStream[i]-textStream[i]+1;
}
}
}
}
printf(“字母出现的次数\n\n”);
如果(上传者[0]>0)
{
printf(“A:\t%d\n”,upleters[0]);
}
如果(上传者[1]>0)
{
printf(“B:\t%d\n”,upleters[1]);
}
如果(上传者[2]>0)
{
printf(“C:\t%d\n”,upleters[2]);
}
如果(上传者[3]>0)
{
printf(“D:\t%D\n”,upleters[3]);
}
如果(上传者[4]>0)
{
printf(“E:\t%d\n”,upleters[4]);
}
如果(上传者[5]>0)
{
printf(“F:\t%d\n”,upleters[5]);
}
如果(上传者[6]>0)
{
printf(“G:\t%d\n”,upleters[6]);
}
如果(上传者[7]>0)
{
printf(“H:\t%d\n”,upleters[7]);
}
如果(上传者[8]>0)
{
printf(“I:\t%d\n”,upleters[8]);
}
如果(上传者[9]>0)
{
printf(“J:\t%d\n”,upleters[9]);
}
如果(上传者[10]>0)
{
printf(“K:\t%d\n”,upleters[10]);
}
如果(上传者[11]>0)
{
printf(“L:\t%d\n”,upleters[11]);
}
如果(上传者[12]>0)
{
printf(“M:\t%d\n”,upleters[12]);
}
如果(上传者[13]>0)
{
printf(“N:\t%d\N”,upleters[13]);
}
如果(上传者[14]>0)
{
printf(“O:\t%d\n”,upleters[14]);
}
如果(上传者[15]>0)
{
printf(“P:\t%d\n”,upleters[15]);
}
如果(上传者[16]>0)
{
printf(“Q:\t%d\n”,upleters[16]);
}
如果(上传者[17]>0)
{
printf(“R:\t%d\n”,upleters[17]);
}
如果(上传者[18]>0)
{
printf(“S:\t%d\n”,upleters[18]);
}
如果(上传者[19]>0)
{
printf(“T:\T%d\n”,upleters[19]);
}
如果(上传者[20]>0)
{
printf(“U:\t%d\n”,upleters[20]);
}
如果(上传者[21]>0)
{
printf(“V:\t%d\n”,upleters[21]);
}
如果(上传者[22]>0)
{
printf(“W:\t%d\n”,upleters[22]);
}
如果(上传者[23]>0)
{
printf(“X:\t%d\n”,upleters[23]);
}
如果(上传者[24]>0)
{
printf(“Y:\t%d\n”,upletter[
#include <stdio.h>
#include <string>
#include <ctype.h>
int main()
{
int strLength;
int *ptr_strLength;
char textStream[];
int cont = 1;
while (cont != 0)
{
scanf("%s", textStream[]);
strLength = strlen(textStream[]);
}
ptr_strLength = &strLength;
void findLetters(ptr_strLength);
)
void findLetters(int *ptr) //find occurences of all letters
{
int upLetters[26];
int loLetters[26];
char text[];
int index;
int a = 65;
int lowA = 97;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
int i = 0;
int length;
length = *ptr;
while (textStream[i] != '\0')
{
for (; i < length ); i++)
{
if (isalpha(textStream[i])) //checks if char is an alphabet
{
for (; a < 123); a++)
{
if (textStream[i] = a)
{
if (textStream[i] > 64 && textStream[i] < 91) //stores occurrences of uppercase letters
{
textStream[i] - a = index;
upLetters[index] = upLetters[index] + textStream[i] - textStream[i] + 1;
}
if (textStream[i] > 89 && textStream[i] < 123) //stores occurrences of lowercase letters
{
textStream[i] - lowA = index;
loLetters[index] = loLetters[index] + textStream[i] - textStream[i] + 1;
}
}
}
}
printf("Number of Occurrences of letters\n\n");
if(upLetters[0] > 0)
{
printf("A : \t%d\n", upLetters[0]);
}
if(upLetters[1] > 0)
{
printf("B : \t%d\n", upLetters[1]);
}
if(upLetters[2] > 0)
{
printf("C : \t%d\n", upLetters[2]);
}
if(upLetters[3] > 0)
{
printf("D : \t%d\n", upLetters[3]);
}
if(upLetters[4] > 0)
{
printf("E : \t%d\n", upLetters[4]);
}
if(upLetters[5] > 0)
{
printf("F : \t%d\n", upLetters[5]);
}
if(upLetters[6] > 0)
{
printf("G : \t%d\n", upLetters[6]);
}
if(upLetters[7] > 0)
{
printf("H : \t%d\n", upLetters[7]);
}
if(upLetters[8] > 0)
{
printf("I : \t%d\n", upLetters[8]);
}
if(upLetters[9] > 0)
{
printf("J : \t%d\n", upLetters[9]);
}
if(upLetters[10] > 0)
{
printf("K : \t%d\n", upLetters[10]);
}
if(upLetters[11] > 0)
{
printf("L : \t%d\n", upLetters[11]);
}
if(upLetters[12] > 0)
{
printf("M : \t%d\n", upLetters[12]);
}
if(upLetters[13] > 0)
{
printf("N : \t%d\n", upLetters[13]);
}
if(upLetters[14] > 0)
{
printf("O : \t%d\n", upLetters[14]);
}
if(upLetters[15] > 0)
{
printf("P : \t%d\n", upLetters[15]);
}
if(upLetters[16] > 0)
{
printf("Q : \t%d\n", upLetters[16]);
}
if(upLetters[17] > 0)
{
printf("R : \t%d\n", upLetters[17]);
}
if(upLetters[18] > 0)
{
printf("S : \t%d\n", upLetters[18]);
}
if(upLetters[19] > 0)
{
printf("T : \t%d\n", upLetters[19]);
}
if(upLetters[20] > 0)
{
printf("U : \t%d\n", upLetters[20]);
}
if(upLetters[21] > 0)
{
printf("V : \t%d\n", upLetters[21]);
}
if(upLetters[22] > 0)
{
printf("W : \t%d\n", upLetters[22]);
}
if(upLetters[23] > 0)
{
printf("X : \t%d\n", upLetters[23]);
}
if(upLetters[24] > 0)
{
printf("Y : \t%d\n", upLetters[24]);
}
if(upLetters[25] > 0)
{
printf("Z : \t%d\n", upLetters[25]);
}
if(loLetters[0] > 0)
{
printf("a : \t%d\n", upLetters[0]);
}
if(loLetters[1] > 0)
{
printf("b : \t%d\n", upLetters[1]);
}
if(loLetters[2] > 0)
{
printf("c : \t%d\n", upLetters[2]);
}
if(loLetters[3] > 0)
{
printf("d : \t%d\n", upLetters[3]);
}
if(loLetters[4] > 0)
{
printf("e : \t%d\n", upLetters[4]);
}
if(loLetters[5] > 0)
{
printf("f : \t%d\n", upLetters[5]);
}
if(loLetters[6] > 0)
{
printf("g : \t%d\n", upLetters[6]);
}
if(loLetters[7] > 0)
{
printf("h : \t%d\n", upLetters[7]);
}
if(loLetters[8] > 0)
{
printf("i : \t%d\n", upLetters[8]);
}
if(loLetters[9] > 0)
{
printf("j : \t%d\n", upLetters[9]);
}
if(loLetters[10] > 0)
{
printf("k : \t%d\n", upLetters[10]);
}
if(loLetters[11] > 0)
{
printf("l : \t%d\n", upLetters[11]);
}
if(loLetters[12] > 0)
{
printf("m : \t%d\n", upLetters[12]);
}
if(loLetters[13] > 0)
{
printf("n : \t%d\n", upLetters[13]);
}
if(loLetters[14] > 0)
{
printf("o: \t%d\n", upLetters[14]);
}
if(loLetters[15] > 0)
{
printf("p : \t%d\n", upLetters[15]);
}
if(loLetters[16] > 0)
{
printf("q : \t%d\n", upLetters[16]);
}
if(loLetters[17] > 0)
{
printf("r : \t%d\n", upLetters[17]);
}
if(loLetters[18] > 0)
{
printf("s : \t%d\n", upLetters[18]);
}
if(loLetters[19] > 0)
{
printf("t : \t%d\n", upLetters[19]);
}
if(loLetters[20] > 0)
{
printf("u : \t%d\n", upLetters[20]);
}
if(loLetters[21] > 0)
{
printf("v : \t%d\n", upLetters[21]);
}
if(loLetters[22] > 0)
{
printf("w : \t%d\n", upLetters[22]);
}
if(loLetters[23] > 0)
{
printf("x : \t%d\n", upLetters[23]);
}
if(loLetters[24] > 0)
{
printf("y : \t%d\n", upLetters[24]);
}
if(loLetters[25] > 0)
{
printf("z : \t%d\n", upLetters[25]);
}
}
}
#include <stdio.h>
#include <stdlib.h>
#define COUNTSZ 64
#define ALPHASZ 26
#define NOFFSET (ALPHASZ) * 2
void analizefile (char *fn, int *cnt);
void prnanalysis (int *cnt);
int main (int argc, char **argv)
{
int count[COUNTSZ] = {0};
char *fn = argc > 1 ? argv[1] : NULL;
analizefile (fn, count);
prnanalysis (count);
return 0;
}
void analizefile (char *fn, int *cnt)
{
FILE *fp = NULL;
int c, numbers, spaces, special;
c = numbers = spaces = special = 0;
if (fn) {
if (!(fp = fopen (fn, "r"))) {
fprintf (stderr, "%s() error: file open failed '%s'.\n", __func__, fn);
exit (EXIT_FAILURE);
}
}
else
fp = stdin;
while ((c = fgetc (fp)) != EOF)
{
if (c >= 'A' && c <= 'Z')
cnt[c - 'A']++;
else if (c >= 'a' && c <= 'z')
cnt[c - 'a' + ALPHASZ]++;
else if (c >= '0' && c <= '9')
cnt[c - '0' + NOFFSET]++;
else if ( c == ' ' )
cnt[COUNTSZ - 2]++;
else
cnt[COUNTSZ - 1]++;
}
if (fp != stdin) fclose (fp);
}
void prnanalysis (int *cnt)
{
int i = 0;
char *set = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01123456789";
printf ("\nThe frequency analysis of the input characters:\n\n");
printf (" Spaces : %d\n Special : %d\n\n", cnt[COUNTSZ - 2], cnt[COUNTSZ - 1]);
printf ("Alphabet Characters:\n");
for (i = 0; i < ALPHASZ; i++)
printf (" %c : %4d %c : %4d\n",
set[i], cnt[i], set[i + ALPHASZ], cnt[i + ALPHASZ]);
printf ("\nNumbers:\n");
for (i = ALPHASZ * 2; i < COUNTSZ - 2; i++)
printf (" %c : %d\n", set[i], cnt[i]);
printf ("\n");
}
$ ./bin/charcount_case <~/doc/bash/ascii-chars.txt
The frequency analysis of the input characters:
Spaces : 1106
Special : 318
Alphabet Characters:
A : 1 a : 16
B : 1 b : 13
C : 5 c : 25
D : 5 d : 15
E : 1 e : 26
F : 1 f : 10
G : 1 g : 2
H : 5 h : 7
I : 1 i : 2
J : 1 j : 1
K : 1 k : 3
L : 1 l : 6
M : 1 m : 2
N : 1 n : 8
O : 5 o : 4
P : 1 p : 3
Q : 1 q : 2
R : 1 r : 7
S : 1 s : 14
T : 1 t : 11
U : 1 u : 4
V : 1 v : 2
W : 1 w : 1
X : 1 x : 135
Y : 1 y : 2
Z : 1 z : 1
Numbers:
0 : 400
1 : 183
1 : 89
2 : 81
3 : 81
4 : 80
5 : 80
6 : 80
7 : 31
8 : 31