c - C的python中是否有等效于 "in"关键字的关键字

标签 c python-3.x string substring

我正在尝试检查某个关键字是否在用户输入的字符串中。

这就是我在 python 中的做法。

keyword = "something"
user_input = input("Enter a something: ")
if keyword in user_input: # I don't know how to do this part
    print("You entered something")
else:
    print("You didn't enter something")

我如何在 C 中做类似的事情?

最佳答案

您可以使用 strstr()在字符串中搜索子字符串。它不像 Python 的 in 运算符那样通用(例如,strstr 不能用于检查给定值是否存储在数组中),但它将解决您提出的问题。

例如(未经测试):

const char *keyword = "something";
const char *user_input = input("Enter a something: "); // I'll leave this to you to implement
if (NULL != strstr(user_input, keyword)) {
    printf("You entered something\n");
} else {
    printf("You didn't enter something\n");
}

关于c - C的python中是否有等效于 "in"关键字的关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56784322/

相关文章:

c - printf ("%d"的输出)没有附加参数

c - 在 for 循环之外使用变量。

python-3.x - 将Tkinter转换为exe

javascript字符串替换多个特殊字符

HTML 样式的 PHP 字符串

java - 使用嵌套 for 循环将字符串数组中的字符替换为键盘左侧或右侧的相应字符

c - 为 UDP 套接字的 recv fcn 设置超时

c++ - 用 C/C++ 编写的语法高亮库

python-3.x - 如何在python3中导入cv2?

python - 从多个文本文件中删除段落