0

我正在将程序从普通数组表示法更改为纯指针表示法,并且我无法在 while 循环中使用 getchar() 接收用户输入。我打印出程序正在接收并输出倒置的问号。我不确定为什么会发生这种情况,因为我从未更改过变量类型。问题在于接收用户输入的第二个函数。感谢您的帮助。

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

/* Function prototypes */
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;
}

/* Function to generate a random string of 40 uppercase letters */
void fillS1(char randomlyGeneratedString[])
{
char * pointerToRandom = randomlyGeneratedString;
int i;

for (i = 0; i < 40; i++) {
    *(pointerToRandom + i) = 'A' + rand() % 26;
}
pointerToRandom[40] = (char)0;

//printf("This is the generated string %s\n", pointerToRandom);

}

/* Function to get user input of characters */
void fillS2(char userString[], char randomString[], char replacementCharacter)
{

char * pointerToUserString = userString;
char * pointerToRandom = randomString;


int i = 0;
int n = 0;
int capitalLetterCheck = 0;

char loopContinue = 0;
char copyString[42];

char * pointerToCopyString = copyString; 

fillS1(pointerToRandom);

do {

    /* For loop to copy the first randomly generated string */
    for(i = 0; i < 42; i++)
        *(pointerToCopyString + i) = *(pointerToRandom + i);

    i = 0;
    capitalLetterCheck = 0;

    /* While loop to to get user input */
    printf("Please enter at least 2 capital letters and a maximum of 20.\n");
    while  (((*(pointerToUserString + i)) = getchar() != '\n'))  {

        /* Counter to determine how many characters were entered */
        i++;

    }

    i++;

    *(pointerToUserString + i) = '\0';

    //printf("This is the value if i %i", i);
    //printf("This is the user's string %s", pointerToUserString);

    /* Capital letter check */
    for (n = 0; n < 20; n++) {
        if (((*(pointerToUserString + i)) >= 'A') && (*(pointerToUserString + i) <= 'Z'))                  
   {          
            capitalLetterCheck++;
        }
    }

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

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

    else if (capitalLetterCheck >= 2) {

        puts(pointerToUserString);

        printf("Enter a character to replace occuring letters.\n");
        scanf("%c", &replacementCharacter);
        getchar();

        strFilter(pointerToCopyString, pointerToUserString, replacementCharacter);

    }

    else

        printf("You  must have 2 capital letters.\n");


    printf("Would you like to enter another string (y/n)?\n");
    loopContinue = getchar();
    getchar();


} while (loopContinue != 'n' && loopContinue != 'N');

}

/* Function to replace letters with the character chosen by the user */
void strFilter(char a[], char b[], char c)
{
char * pointerToA = a;
char * pointerToB = b;


int i = 0;
int n = 0;

while (n < 20) {

    for (i = 0; i < 40; i++) {
        if ((*(pointerToA + i)) == *(pointerToB + n)){
            *(pointerToA + i) = c;
        }
    }
    i = 0;
    n++;
}

puts(a);
}
4

1 回答 1

5

问题是在if声明中:

(*(pointerToUserString + i)) = getchar() != '\n'

赋值是一个表达式,其优先级低于比较。这意味着上述内容与以下内容相同:

(*(pointerToUserString + i)) = (getchar() != '\n')

所以被分配了不是你想要(*(pointerToUserString + i))的表达式的值。getchar() != '\n'

于 2012-11-06T08:14:32.903 回答