Comparing elements of an array to a char

V

Varun Sinha

I have an array of chars. I want to loop through it, compare each
element to different chars and depending on which character it
matches, take some action.

My problem is that I can't figure out how to compare array elements to
chars.

I wanted it to be something like this

if(array is 'A')
printf("BlahA");
else if((array is 'B') or (array is 'C'))
printf("BlahBC");
.....
.....
.....

I've been struggling with this for a while, and everytime would come
up with some ugly, makeshift hack to get the job done. But this time,
I needed it to do specifically this and couldn't find anything on the
net.

Thanks for the help!!!

Varun
 
J

Joona I Palaste

Varun Sinha said:
I have an array of chars. I want to loop through it, compare each
element to different chars and depending on which character it
matches, take some action.
My problem is that I can't figure out how to compare array elements to
chars.
I wanted it to be something like this
if(array is 'A')
printf("BlahA");
else if((array is 'B') or (array is 'C'))
printf("BlahBC");


Change each occurrence of "is" to == and each occurrence of "or" to
|| and you've pretty much got the C code.
....
....
....
I've been struggling with this for a while, and everytime would come
up with some ugly, makeshift hack to get the job done. But this time,
I needed it to do specifically this and couldn't find anything on the
net.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"And according to Occam's Toothbrush, we only need to optimise the most frequent
instructions."
- Teemu Kerola
 
A

Al Bowers

Joona said:
Varun Sinha said:
I have an array of chars. I want to loop through it, compare each
element to different chars and depending on which character it
matches, take some action.

My problem is that I can't figure out how to compare array elements to
chars.

I wanted it to be something like this

if(array is 'A')
printf("BlahA");
else if((array is 'B') or (array is 'C'))
printf("BlahBC");



Change each occurrence of "is" to == and each occurrence of "or" to
|| and you've pretty much got the C code.


Yeah. Pretty much. But you should also change the "printf"s to
"puts".
 
R

Richard Bos

Al Bowers said:
Joona said:
Varun Sinha said:
I have an array of chars. I want to loop through it, compare each
element to different chars and depending on which character it
matches, take some action.
My problem is that I can't figure out how to compare array elements to
chars.
I wanted it to be something like this
if(array is 'A')
printf("BlahA");
else if((array is 'B') or (array is 'C'))
printf("BlahBC");


Change each occurrence of "is" to == and each occurrence of "or" to
|| and you've pretty much got the C code.


Yeah. Pretty much. But you should also change the "printf"s to
"puts".


Why? It doesn't have anything whatsoever to do with the original
question, and it changes the behaviour of the program, quite possibly in
an unwanted way.

Richard
 
S

sahukar praveen

Richard Bos said:
Al Bowers said:
Joona said:
Varun Sinha <[email protected]> scribbled the following:

I have an array of chars. I want to loop through it, compare each
element to different chars and depending on which character it
matches, take some action.

My problem is that I can't figure out how to compare array elements to
chars.

I wanted it to be something like this

if(array is 'A')
printf("BlahA");
else if((array is 'B') or (array is 'C'))
printf("BlahBC");

Change each occurrence of "is" to == and each occurrence of "or" to
|| and you've pretty much got the C code.


Yeah. Pretty much. But you should also change the "printf"s to
"puts".


Why? It doesn't have anything whatsoever to do with the original
question, and it changes the behaviour of the program, quite possibly in
an unwanted way.

Richard


switch(){ } can be a better (clean) candidate for your requirement.

char ch;
swicth(ch)
{
case 'A':
printf(" ");
break;
case 'B':
printf(" ");
break;
}

Thanks
Praveen Kumar
 
E

Ed Morton

Joona said:
Varun Sinha said:
I have an array of chars. I want to loop through it, compare each
element to different chars and depending on which character it
matches, take some action.

My problem is that I can't figure out how to compare array elements to
chars.

I wanted it to be something like this

if(array is 'A')
printf("BlahA");
else if((array is 'B') or (array is 'C'))
printf("BlahBC");



Change each occurrence of "is" to == and each occurrence of "or" to
|| and you've pretty much got the C code.


It'd probably be tidier with a switch statement. I also suspect there's
more to the OPs confusion than was posted since I can't believe he just
didn't know how to use "==" and "||" especially since s/he's come up
with some "ugly makeshift" alternative, so here's a compilable code segment:

#include <stdio.h>

int main(int argc, char* argv[])
{
char array[] = "ABCD";
int i;
char c;

for (i = 0; i < sizeof array - 1; i++) {
c = array;
switch(c) {
case 'A': printf("BlahA: %c\n",c);
break;
case 'B': /* fall through */
case 'C': printf("BlahBC: %c\n",c);
break;
default: printf("Unexpected character: %c\n",c);
break;
}
}

return 0;
}

If the input array is a string, you could use strlen() instead of sizeof
- 1 as the loop terminator.

Regards,

Ed.
 
A

Al Bowers

Richard said:
Joona I Palaste wrote:

Varun Sinha <[email protected]> scribbled the following:


I have an array of chars. I want to loop through it, compare each
element to different chars and depending on which character it
matches, take some action.

My problem is that I can't figure out how to compare array elements to
chars.

I wanted it to be something like this

if(array is 'A')
printf("BlahA");
else if((array is 'B') or (array is 'C'))
printf("BlahBC");

Change each occurrence of "is" to == and each occurrence of "or" to
|| and you've pretty much got the C code.


Yeah. Pretty much. But you should also change the "printf"s to
"puts".



Why? It doesn't have anything whatsoever to do with the original
question, and it changes the behaviour of the program, quite possibly in
an unwanted way.


I thought it was worth mentioning.
puts is an easy fix for a potential problem.
It is quite possible that with just printf without fflush(stdout)
or a temrninating newline character that problems will continue.
 
R

Richard Bos

Al Bowers said:
I thought it was worth mentioning.
puts is an easy fix for a potential problem.

No, it _would_ be, if it did the same. It doesn't.
It is quite possible that with just printf without fflush(stdout)
or a temrninating newline character that problems will continue.

Then suggest that the OP call fflush(stdout); that, at least, doesn't
change the output of the program in unwanted ways.

Richard
 
V

Varun Sinha

Thanks for the help!! I got it to work. I was wrong about using the ==
operator with strings.

Thanks to all!!

Varun
 
I

Irrwahn Grausewitz

Thanks for the help!! I got it to work. I was wrong about using the ==
operator with strings.

Well, you still cannot use == to compare strings, but of course you
can use == to compare single characters, which is what you want to do.

Regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top