gpt4 book ai didi

c - 字符串问题

转载 作者:行者123 更新时间:2023-11-30 18:06:23 25 4
gpt4 key购买 nike

我遇到了一个尚未解决的问题。我的程序需要像这样工作:

Put some string: Hello World Hello World World World
output: 4

程序获取字符串和子字符串,需要在字符串中找到子字符串并统计字符串中最常见的子字符串。

我写了一些代码但没有成功..

int main()
{
char string[10];
int i=0,x=0;
char find[] = "hello";
gets(string);
while(string[i] != 0)
if(string[i] == find[i]))
x++;
printf("%d", x);
i++;
return 0;
}

最佳答案

我认为你最好使用字符串库调用,例如

char *strstr( const char *s1, const char *s2)这将返回一个指向 s1 中字符串 s2 的第一个实例的指针。如果 s1 中未遇到 s2,则返回 NULL 指针。

您可以在这里找到它们的列表: http://www.edcc.edu/faculty/paul.bladek/c_string_functions.htm

关于c - 字符串问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5556585/

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