gpt4 book ai didi

无法在函数中进行函数调用以正常工作

转载 作者:太空宇宙 更新时间:2023-11-04 06:37:22 25 4
gpt4 key购买 nike

我正在编写一个程序来生成一串随机大写字母,然后获取用户输入的大写字母以及用户输入的字符。对于随机字符串中用户输入字母的任何实例,它将用用户输入的字符替换该字母。

例如,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);
}

谢谢。

最佳答案

首先,请尝试让您的代码更易于阅读,我不是在谈论缩进,而是在谈论它的流程。

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

编码时需要注意以下几点:

  • 给你的变量和函数明确的名字,特别是如果你要让别人在某个时候阅读你的代码
  • 当你有一个特定的任务要执行时(获取用户的输入,生成一个随机字符串等),通过编写小函数来保持你的代码流程简单,而不是仅仅将大部分代码写在重叠的循环中
  • 您还可以查看 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

这是一个如何组织代码的示例(尽管不要使用它 - 没有释放,没有获得用户的输入等):

#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”的值,然后再看看如何获​​取用户的输入。

关于无法在函数中进行函数调用以正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13215864/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com