``````#include <iostream>
#include <string>
/*i want to compare string and char as char is a subset of string*/
string S ;
//algorithm is somewhat like:
i=0;
while(i<23){
char c = (char) i;
S[i] = c;
}
for(i=23;i<50;i++){
S[i]="hello";
}
//i want to check how many hello are there
i=0;
x=0;
while(i<50){
if (S[i] = "hello"){
x++;
i++;}
else{
i++;
}
//but here is one problem the array elements from 0 to 22 are characters so it cant be compared
//with hello which is a string. to do so i have to convert those characters into strings.
//code would be somewhat
i=0;
while(i<23){
c = char(i)
//convert c which is a character into string
S[i] = c;
}
``````

## Recommended Answers

string's operator `==` can take a `string` and a `char` so all you have to do is:

``````i=0;
while(i<23){
c = char(i)
//convert c which is a character into string
if (S[i] == c)
// do something here
}
``````

## All 3 Replies

string's operator `==` can take a `string` and a `char` so all you have to do is:

``````i=0;
while(i<23){
c = char(i)
//convert c which is a character into string
if (S[i] == c)
// do something here
}
``````

Just a comment, but if you have an array of strings, you can iterate through them with one of the std algorithms to count the ones that you want. So, to count the number of "hello" strings, you can simply do:

``````auto number_of_hellos = std::count( S.cbegin(), S.cend(), "hello" );
``````

It's generally best to look for a standard algorithm to do something if you can :)

@NAthan oliver
I have to check for "hello" not (char) c;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.