2016-12-26 67 views
1

我想了解为什么我的代码不会给我想要的结果。 它应该生成一些随机字母,然后应该计算每个字母生成的次数。我的预计产量将与此类似正确的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; 
} 
+0

请注明预期结果和实际结果的示例。 – kaylum

+1

'星号[M]; strcpy(星号,“这些”);'你在那里做什么? 'M'是'5',但''这些''需要7个字符来存储。然后,显然任何进一步的'strcat'到'asterisk'都会继续溢出缓冲区。 – kaylum

+0

我已经纠正了,对不起,我是在试图了解它是如何工作灭绝了的版本我复制它,而不是一个我应该有,我现在已经修改它 – mz1993

回答

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; 
}