gpt4 book ai didi

c - 如何在c中检查一行文本中的某些字符

转载 作者:行者123 更新时间:2023-11-30 17:17:19 27 4
gpt4 key购买 nike

我有一个学校项目的代码,我有 2 个问题。第一个是我的代码在第二次循环后崩溃,就在“输入要转换的短语”之后。

我的第二个问题是,我需要添加一个部分,如果 str 中的任何单词以元音开头,则以 -way 结尾并保留第一个字母。 EX apple = appleway,但如果它不是元音;运行 Piglatinwork()

#include < stdio.h >
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
//calls and functon prototypes
int printlatinwork (char *thiss);
int main ()
{
char again;

do {
char *thiss;
char str[20];
/* prompt the user to enter his phrase */
printf ("please enter the phrase to be converted\n");
str[20] = gets (str);

/* THIS LOOPS THRU TOKENS AND PRINTS */
thiss = strtok (str, " ");
if (thiss != NULL);
{
printlatinwork (thiss);
thiss = strtok (NULL, " ");
}

printf ("\ngo again? enter y to go again\n");
scanf ("%c", &again);

} while (again == 'y');

return 0;
}

/* FUNCTION TO PRINT, CALLED THERE */
printlatinwork (char *thiss)
{
printf ("%s%c%s ", thiss + 1, thiss[0], "ay");
return;
}

最佳答案

你搞砸了。首先不要使用gets。它不再是 libc 的一部分并且非常不安全。接下来,在初始声明和初始化之后,您不能使用 = 分配字符串。 始终初始化所有变量也是一个好主意。 (尝试读取/使用未初始化的值是未定义行为)

有多种使用strtok的方法。最有效的方法之一就是使用 for 循环。初始条件使用字符串变量的名称,增量使用NULL。测试条件很简单,就是返回值不为NULL。当使用 strtok 时,它将破坏原始字符串(通过在其中嵌入空终止字符)。虽然代码中没有要求,但最好始终向 strtok 发送副本。 (如果 str 是动态分配的,则这是必需的)此外,注意:strtok 返回(指向当前 token 的指针)为 <仅适用于当前迭代。如果您需要保留 token ,也请复制一份。

接下来,当您编译程序时,请始终在启用警告的情况下进行编译。至少gcc -Wall -o progname yourfile.c。您还可以添加-Wextra

这是您的代码的更新版本。看一下,如果您有疑问请告诉我:

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

#define MAXS 256

/* calls and functon prototypes */
void printlatinwork (char *thiss);

int main (void)
{
char again = 'n';

do {
char *thiss = 0;
char str[MAXS] = {0};
char copy[MAXS] = {0};
size_t nchr = 0;
int c = 0;

/* prompt the user to enter his phrase */
printf ("please enter the phrase to be converted: ");
// str[20] = gets (str); /* NEVER USE gets */
fgets (str, MAXS - 1, stdin);

/* get the length of str */
nchr = strlen (str);

/* strip newline or carriage rtn */
while (nchr > 0 && (str[nchr-1] == '\n' || str[nchr-1] == '\r'))
str[--nchr] = 0;

/* strtok destroys original string, make copy */
strncpy (copy, str, strlen (str));

/* THIS LOOPS THRU TOKENS AND PRINTS */
for (thiss = strtok (copy, " "); thiss != NULL; thiss = strtok (NULL, " "))
printlatinwork (thiss);

printf ("\ngo again? enter y to go again\n");
scanf ("%c", &again);

/* always flush the input buffer after calling scanf
or craft a format string that consumes the newline
that remains in stdin (created by pressing [Enter]) */
while ((c = getchar()) != '\n' && c != EOF);

} while (again == 'y');

return 0;
}

/* FUNCTION TO PRINT, CALLED THERE */
void printlatinwork (char *thiss)
{
printf ("%s%c%s ", thiss + 1, thiss[0], "ay");
}

输出

$ ./bin/findchar
please enter the phrase to be converted: this is my phrase
histay siay ymay hrasepay
go again? enter y to go again
y
please enter the phrase to be converted: this is my phrase
histay siay ymay hrasepay
go again? enter y to go again
n

注意printlatinwork中,您应该检查thiss的长度是否大于1以防止>未定义读取字符串末尾(不好)。

关于c - 如何在c中检查一行文本中的某些字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29442089/

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