首页 > 解决方案 > 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++;
}

标签: cstringchar

解决方案


"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");

推荐阅读