在C - 检查是否在字符数组存在一个char [英] In C - check if a char exists in a char array
本文介绍了在C - 检查是否在字符数组存在一个char的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想检查一个字符属于无效字符列表/数组。
I'm trying to check if a character belongs to a list/array of invalid characters.
这是一个Python背景的,我以前可以,只是说:
Coming from a Python background, I used to be able to just say :
for c in string:
if c in invalid_characters:
#do stuff, etc
我怎样才能做到这一点与常规的C字符数组?
How can I do this with regular C char arrays?
推荐答案
的等价的C code是这样的:
The equivalent C code looks like this:
#include <stdio.h>
#include <string.h>
// This code outputs: h is in "This is my test string"
int main(int argc, char* argv[])
{
const char *invalid_characters = "hz";
char *mystring = "This is my test string";
char *c = mystring;
while (*c)
{
if (strchr(invalid_characters, *c))
{
printf("%c is in \"%s\"\n", *c, mystring);
}
c++;
}
return 0;
}
注意INVALID_CHARACTERS是C字符串,即得。一个空值终止字符
阵列。
这篇关于在C - 检查是否在字符数组存在一个char的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文