我正在尝试修改我以前使用纯指针表示法编写的程序。它是一个生成40个大写字母的随机字符串,最多输入20个大写字母和一个字符的程序。程序用输入的字符替换生成的字符串中重复出现的字符。现在,我正在尝试传递随机生成的字符串的参数,以便我可以在第二个函数中访问它们,但不知道该怎么做。
感谢您的帮助。
#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[41];
char s2[21];
char x = 0;
char * pointerToS1;
char * pointerToS2;
pointerToS1 = s1;
pointerToS2 = s2;
fillS2(pointerToS2, pointerToS1, x);
return 0;
}
/* Function to generate a random string of 40 uppercase letters */
void fillS1(char * randomlyGeneratedPointer)
{
char randomlyGeneratedArray[41];
randomlyGeneratedPointer = randomlyGeneratedArray;
int i;
for (i = 0; i < 40; i++) {
*(randomlyGeneratedPointer + i) = 'A' + rand() % 26;
}
}
/* Function to get user input of characters */
void fillS2(char * userStringPointer, char * randomStringPointer, char replacementCharacter)
{
char userstring[21];
char randomString[41];
char copyString[42];
//char * pointerToCopyString = copyString;
userStringPointer = userstring;
int i = 0;
int n = 0;
int lowercaseCheck = 0;
char loopContinue = 0;
fillS1(randomStringPointer); //here is the function call for the randomly generated string.
printf("This is the random string: %s", randomStringPointer);
do {
/* For loop to copy the first randomly generated string */
for(i = 0; i < 42; i++)
*(randomStringPointer + i) = copyString[i];
randomStringPointer = copyString;
i = 0;
lowercaseCheck = 0;
/* While loop to to get user input */
printf("Please enter at least 2 capital letters and a maximum of 20.\n");
while ((((*(userStringPointer + i)) = getchar()) != '\n')) {
/* Counter to determine how many characters were entered */
i++;
}
/* Adding 1 to add to add null character */
i++;
*(userStringPointer + i) = '\0';
//printf("This is the user's string %s", userStringPointer);
/* Capital letter check */
for (n = 0; n < 20; n++) {
if (((*(userStringPointer + n)) >= 'a') && (*(userStringPointer + n) <= 'z')) {
lowercaseCheck++;
}
}
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 (lowercaseCheck == 0) {
puts(userStringPointer);
printf("Enter a character to replace occuring letters.\n");
scanf("%c", &replacementCharacter);
getchar();
//printf("this is the copy string before strFilter: %s", randomStringPointer);
//printf("This is the replacement character %c", replacementCharacter);
strFilter(randomStringPointer, userStringPointer, 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 * replacementCopyStringPointer, char * replacementUserStringPointer, char c)
{
int i = 0;
int n = 0;
while (n < 20) {
for (i = 0; i < 40; i++) {
if ((*(replacementCopyStringPointer + i)) == *(replacementUserStringPointer + n)){
*(replacementCopyStringPointer + i) = c;
}
}
i = 0;
n++;
}
puts(replacementCopyStringPointer);
}