我想了解爲什麼我的代碼不會給我想要的結果。 它應該生成一些隨機字母,然後應該計算每個字母生成的次數。我的預計產量將與此類似正確的strcat用法,'asterisk'var被破壞
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(星號[我],「」);到 - > strcat(星號,「」) 和我沒有任何錯誤了,但我不明白我怎麼能規範 「*」在正確的金額(它應該等於次數信露面) 什麼我越來越 -
隨機字母:C
隨機字母:C
隨機字母:乙
隨機字母:乙
個隨機字母:C
一個:*****
B:*****
C:*****
什麼,我想GET-
一個:
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;
}
------------------------第三updtae ----------------- -----------------
所以我添加了一個函數來幫助我控制像我想要的outpout,但我不斷收到錯誤,如 - 「strcat'undefined;假設EXTERN返回int「 或」初始化「:‘字符’中的間接水平不同於」燒焦[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;
}
請註明預期結果和實際結果的示例。 – kaylum
'星號[M]; strcpy(星號,「這些」);'你在那裏做什麼? 'M'是'5',但''這些''需要7個字符來存儲。然後,顯然任何進一步的'strcat'到'asterisk'都會繼續溢出緩衝區。 – kaylum
我已經糾正了,對不起,我是在試圖瞭解它是如何工作滅絕了的版本我複製它,而不是一個我應該有,我現在已經修改它 – mz1993