gpt4 book ai didi

c - 检查字符串卡住的函数

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

该程序应该使用 Hydrox 函数来检查输入的字符串,看看它是否以“oh”(或“ho”)结尾,如果是,则返回 1 值。这是我写的,当我运行该程序时,它就卡住了。我还是一个初学者,所以如果这是一个愚蠢的问题,我提前道歉。

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

#define MAX_LEN 10

int hydroxide(char *compound);

int main(void)
{
char compound[MAX_LEN];
int i, num;

printf("Enter compound> \n");
scanf("%s", compound);

for (i = 0; i < strlen(compound); ++i) {
if (islower(compound[i]))
compound[i] = toupper(compound[i]);
}

num = hydroxide(compound);

printf("%d", num);

return(0);
}

int hydroxide(char *compound)
{
char *end[4], *temp;
int last, status;

last = strlen(compound);

strcpy(end[], &compound[last - 2]);

if (strcmp(end[last - 2],end[last - 1]) > 0) {
temp = end[last - 2];
end[last - 2] = end[last - 1];
end[last - 1] = temp;
}

if (*end[last - 2] == 'H') {
if (*end[last - 1] == 'O')
status = 1;
}

return(status);
}

最佳答案

char end[4], *temp;  /* Change to end[4] */
...
...
strcpy(end, &compound[last - 2]); /* Change to end */
...
...
/* Commented
* if (end[0],end[1]) > 0) {
* temp = end[last - 2];
* end[last - 2] = end[last - 1];
* end[last - 1] = temp;
* }
*/
/* Condition updated */
if ( (end[0] == 'H' && end[1] == 'O') || (end[0] == 'O' && end[1] == 'H') ) {
status = 1;
}

Live Example here

关于c - 检查字符串卡住的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26836905/

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