2012-11-04 43 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