2012-11-04 42 views
0

我正在编写一个程序来生成一个随机大写字母的字符串,然后将用户输入的大写字母与用户的字符一起输入。对于随机字符串中用户输入字母的任何实例,它将用该用户输入的字符替换该字母。无法在函数中调用函数以正常工作

例如,S1 = {BDHFKYL} S2 = {YEIGH} C = '*'

输出= BD * FK * L

直到我说的特征问该方案是正常工作用户输入他们想要替换字母的字符。

输出是:

Please enter at least 2 capital letters and a maximum of 20. 
HDJSHDSHDDS 
HDJSHDSHDDS 
Enter a character to replace occuring letters. 
* 
NWLRBBMQB 
Would you like to enter another string? 

下面的代码:

void fillS1(char x[]); 

void fillS2(char x[], char y[], char z); 

void strFilter(char a[], char b[], char c); 

int main(int argc, const char * argv[]) 
{ 
char s1[42]; 
char s2[22]; 
char x = 0; 

fillS2(s2, s1, x); 

return 0; 
} 

void fillS1(char x[]) 
{ 
for (int i = 0; i < 40; i++) 
    x[i] = 'A' + random() % 26; 
x[40] = (char)0; 
} 

void fillS2(char x[], char y[], char z){ 

char loopContinue = 0; 

do { 

int i = 0; 
int capitalLetterCheck = 0; 

printf("Please enter at least 2 capital letters and a maximum of 20.\n"); 
while ((x[i] = getchar()) != '\n') { 

    i++; 

    } 

x[i] = '\0'; 

if (i < 3) { 
    printf("You need at least two letters\n"); 
} 

else if (i > 21){ 
    printf("You cannot have more than twenty letters\n"); 
} 


for (i = 0; i < 20; i++) { 
     if ((x[i] >= 'a') && (x[i] <= 'z')) { 
      printf("You many only have capital letters.\n"); 
      capitalLetterCheck = 2; 
     } 
    } 


if (capitalLetterCheck != 2) { 
    for (i = 0; i < 20; i++) { 
     if ((x[i] >= 'A') && (x[i] <= 'Z')) { 
      puts(x); 

      fillS1(y); 

      printf("Enter a character to replace occuring letters.\n"); 
      while ((z = getchar() != '\n')) { 

      } 

      strFilter(y, x, z); 
      break; 
     } 
     } 
    } 

    printf("Would you like to enter another string?\n"); 
    gets(&loopContinue); 

} while (loopContinue != 'n'); 

} 

void strFilter(char a[], char b[], char c){ 
int i = 0; 
int n = 0; 

while (n < 20) { 

     for (i = 0; i < 40; i++) { 
      if (a[i] == b[n]){ 
       a[i] = c; 
      } 

    } 
    i = 0; 
    n++; 
} 

    puts(a); 
} 

谢谢。

+0

问题是...? –

+0

这并不是用星星代替人物。 – user1681673

回答

4

首先请尽量让你的代码更容易阅读,而且我不是在谈论缩进,而是在谈论它的流程。

此外,您的示例输出似乎正常工作,因为这里的任何字符串都没有改变...?

有编码时,你应该记住的几件事情:

  • 给你的变量和函数明确的名称,espcecially如果你要有人在某一点看你的代码
  • 尝试通过在执行特定任务(获取用户输入,生成一个随机字符串等)时制作小函数来保持代码的流畅性,而不仅仅是将其大部分写入到叶片式的循环中
  • 您也可以看看scanf(man scanf)得到用户的输入
  • 尝试,当你得到了用户的输入分配的缓冲区,而不是有一个静态的,可能不是正确的大小

这很容易写一些伪代码,然后将其转化成C的:

WHILE someCondition 
    Generate a random string 
    Get a string from the user 
    Get a character from the user 
    Find and replace 
END 

这里是你如何能举办你的代码的一个例子(不使用,虽然它 - 没有任何的FreeS,没有得到用户的输入等):

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

char* generateString(void) 
{ 
    return "AEIOUYAEIOUY"; // In your implementation, this is where you'd generate the random string 
} 

char* getStringInput(void) 
{ 
    return "HELLO"; // In your implementation, this is where you'd get the user's string 
} 

char getCharInput(void) 
{ 
    return '*'; // In your implementation, this is where you'd get the user's character 
} 

char* findAndReplace(char* randomString, char* userString, char userChar) 
{ 
    int l1; 
    int l2; 
    int i; 
    int j; 
    char* output; 

    l1 = strlen(randomString); 
    l2 = strlen(userString); 
    output = (char*)malloc(sizeof(*output) * l1); 
    strcpy(output, randomString); 
    for (i = 0; i < l1; ++i) 
    { 
     for (j = 0; j < l2; ++j) 
      if (randomString[i] == userString[j]) 
       output[i] = userChar; 
    } 

    return (output); 
} 

int main(int ac, char** av) 
{ 
    char* randomString; 
    char* userString; 
    char userChar; 
    char* outputString; 

    randomString = generateString(); 
    userString = getStringInput(); 
    userChar = getCharInput(); 
    outputString = findAndReplace(randomString, userString, userChar); 
    printf("Result: %s\n", outputString); 

    // don't forget to free any allocated buffer 

    return (1); 
} 

多少调试有你做了什么?尝试把一些用printfs在你的代码,看看会发生什么 - 当函数被调用,你有什么变量的值等 例如:

void fillS1(char x[]) 
{ 
printf("-- entering fillS1, buffer value: %s\n", x); 
for (int i = 0; i < 40; i++) 
    x[i] = 'A' + random() % 26; 
x[40] = (char)0; 
printf("-- leaving fillS1, buffer value: %s\n", x); 
} 

(你用printf之前要小心什么在你的缓冲区)

这应该很快告诉你发生了什么问题。

例如,尝试在strFilter调用时检查“c”的值,并再次查看如何获取用户的输入。

+0

+1不直接指向错误,并让OP做功课。 –

+0

谢谢达米安深思熟虑的回应。我为我的缓冲区创建了值的唯一原因是我不允许使用字符串处理函数,并且s1的大小需要为40,而s2的最大大小为20.我将进行一些调试并查看我找到了什么。再次感谢您的帮助。我很感激。 – user1681673

+0

嗨达米安,我一直在研究这个计划,并且已经研究出了大部分的错误,但是有一件事我找不到。用户输入字符串在函数结束时会自行翻倍。是因为我使用getchar()?如果我有选择,我宁愿使用getchar(),因为它可以更容易地检查字符串的大小。谢谢您的帮助。 – user1681673