我想了解为什么我的代码没有给我想要的结果。 它应该生成一些随机字母,然后应该计算每个字母生成的次数。我的预期输出将类似于此
a: *
b: ******
c: **
d: **
e:
f: *
我在理解什么是错误的?
#include <stdio.h>
#include <stdlib.h>
#define N 3
#define lettera 97
#define M 5
int main()
{
int c, i,random[M];
char charray[N], asterisk[M];
// array of random numbers which in turn will genrate the randomnes in our sequence of letters
for (i = 0; i < M; i++)
{
random[i] = rand() % 3;
random[i] = random[i] + lettera +i;
printf("random letters : %c\n", random[i]);
}
//the char array is build of the abc given the constanet N
for (i = 0; i < N; i++)
{
charray[i] = lettera + i;
}
for (i = 0; i < N; i++)
{
if ((random[i] >= lettera) || (random[i] <= lettera + N - 1))
{
for (c = 0; c < M; c++)
if (charray[i] == random[c])
{
strcat(asterisk, "*");
}
}
}
for (i = 0; i < N; i++)
{
printf("%c: %s\n", charray[i], asterisk);
}
return 0;
}
---------更新-------------------
#include <stdio.h>
#include <stdlib.h>
#define N 3
#define lettera 97
#define M 5
#define buff 40
int main()
{
int c, i,random[M];
char charray[N], asterisk[buff] = "";
// array of random numbers which in turn will genrate the randomnes in our sequence of letters
for (i = 0; i < M; i++)
{
random[i] = rand() % 3;
random[i] = random[i] + lettera +i;
printf("random letters : %c\n", random[i]);
}
//the char array is build of the abc given the constanet N
for (i = 0; i < N; i++)
{
charray[i] = lettera + i;
}
for (i = 0; i < N; i++)
{
if ((random[i] >= lettera) || (random[i] <= lettera + N - 1))
{
for (c = 0; c < M; c++)
if (charray[i] == random[c])
{
strcat(asterisk[i], "*");
}
}
}
for (i = 0; i < N; i++)
{
printf("%c: %s\n", charray[i], asterisk[i]);
}
return 0;
}
-----------------------------另一次更新---------------- ------------------
所以我改变了strcat(asterisk [i],“”);到 - &gt; strcat(星号,“”) 我不再有任何错误,但我不明白我怎么能调节 正确数量的“*”(它应该相当于该字母出现的次数) 我得到了什么 -
随机字母:c
随机字母:c
随机字母:b
随机字母:b
随机字母:c
a:*****
b:*****
c:*****
我想得到什么 -
a:
b:**
c:***
我采取什么方法来实现这个目标?
#include <stdio.h>
#include <stdlib.h>
#define N 3
#define lettera 97
#define M 5
#define buff 40
int main()
{
int c, i,random[M];
char charray[N],abc[M], asterisk[buff] = "";
// array of random numbers which in turn will genrate the randomnes in our sequence of letters
for (i = 0; i < M; i++)
{
random[i] = rand() % 3;
random[i] = random[i] + 'a';
printf("random letters : %c\n", random[i]);
}
//the char array is build of the abc given the constanet N
for (i = 0; i < N; i++)
{
charray[i] = lettera + i;
}
for (i = 0; i < N; i++)
{
if ((random[i] >= lettera) || (random[i] <= lettera + N - 1))
{
for (c = 0; c < M; c++)
if (charray[i] == random[c])
{
strcat(asterisk, "*");
}
}
}
for (i = 0; i < N; i++)
{
printf("%c: %s\n", charray[i], asterisk);
}
return 0;
}
------------------------ 3rd updtae --------------------- -------------
所以我添加了一个函数来帮助我像我想要的那样控制输出,但我不断收到错误,例如 - “strcat'undefined;假设extern返回int” 或“初始化”:'char'与'char [1]“的间接等级不同 这次我已经初始化了两个,所以我不明白为什么会发生这种情况..
#include <stdio.h>
#include <stdlib.h>
#define N 3
#define lettera 97
#define M 5
#define buff 40
char aster(int flag, int array[]);
int main()
{
int c, i, random[M], times[M] = { 0 };
char charray[N];
// array of random numbers which in turn will genrate the randomnes in our sequence of letters
for (i = 0; i < M; i++)
{
random[i] = rand() % 3;
random[i] = random[i] + 'a';
printf("random letters : %c\n", random[i]);
}
//the char array is build of the abc given the constanet N
for (i = 0; i < N; i++)
{
charray[i] = lettera + i;
}
for (i = 0; i < N; i++)
{
if ((random[i] >= lettera) || (random[i] <= lettera + N - 1))
{
for (c = 0; c < M; c++)
if (charray[i] == random[c])
{
times[i]++;
}
}
}
for (i = 0; i < N; i++)
{
printf("%c: %s\n", charray[i], aster (i,times));
}
return 0;
}
char aster(int flag,int array[])
{
int i;
char ch="", asterisk[buff] = "";
for (i = 0; i < array[flag]; i++)
{
ch = strcat(asterisk, "*");
}
return ch;
}
答案 0 :(得分:1)
我想我得到了它,只是一个小东西,哪里是通过malloc释放分配内存所需的最佳位置?
#include <stdio.h>
#include <stdlib.h>
#define N 8
#define lettera 97
#define M 20
#define buff 40
char *aster(int flag, int array[]);
int main()
{
int c, i, random[M], times[M] = { 0 };
char charray[N],z;
// array of random numbers which in turn will genrate the randomnes in our sequence of letters
for (i = 0; i < M; i++)
{
random[i] = rand() % 4;
random[i] = random[i] + 'a';
printf("random letters : %c\n", random[i]);
}
//the char array is build of the abc given the constanet N
for (i = 0; i < N; i++)
{
charray[i] = lettera + i;
}
for (i = 0; i < N; i++)
{
if ((random[i] >= lettera) || (random[i] <= lettera + N - 1))
{
for (c = 0; c < M; c++)
if (charray[i] == random[c])
{
times[i]++;
}
}
}
for (i = 0; i < N; i++)
{
printf("%c: %s\n", charray[i], aster(i, times));
}
return 0;
}
char *aster(int flag, int array[])
{
int i,keylen;
char asterisk[buff] = "";
char *key;
for (i = 1; i < array[flag]; i++)
{
strcat(asterisk, "*");
}
/* Initial memory allocation */
key = (char*)malloc(buff * sizeof(char));
strcpy(key, asterisk);
return key;
}