c - How to compare characters in a string in C language
问题描述
char name[]="Rajas";
char ch;
ch=name[0];
if(ch=="R")
printf("Found");
return 0;
Why is this not working? I want to separate vowels and consonants from a string, by comparing their characters. I was able to do this in C++ language by
int i=0;
char name[]="String";
string ch[20];
while(name[i]!='\0')
{
ch[i]=name[i];
i++;
}
解决方案
"R"
is a string literal of type char[2]
- containing 'R'
and '\0'
. That's not what you want to compare with.
You need to compare with char 'R'
:
if(ch == 'R')
printf("Found");
推荐阅读
- python - Python 中的 ModuleNotFoundError
- python - 从列中选择的数值的平均值
- html - CSS - 如何为子元素应用 css plus 选择器
- javascript - 如何在javascript中获取动态加载图像的宽度和高度?
- c++ - C++ STL中set的'insert'函数的原理是什么?
- java - 如何转义 ldap 查询中的特殊字符?
- jquery - 如何在移动设备上向上/向下滚动后“显示/隐藏”Bootstrap 3 内容?
- python - 在 tkinter 窗口之间来回切换
- css - 为什么文本出现在行上
- php - 如何使用 txt 文件的文件列表作为数组变量?