By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,134 Members | 1,942 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,134 IT Pros & Developers. It's quick & easy.

Comparing elements of an array to a char

P: n/a
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[i] is 'A')
printf("BlahA");
else if((array[i] is 'B') or (array[i] 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
Nov 13 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Varun Sinha <vs****@purdue.edu> 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[i] is 'A')
printf("BlahA");
else if((array[i] is 'B') or (array[i] 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 (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"And according to Occam's Toothbrush, we only need to optimise the most frequent
instructions."
- Teemu Kerola
Nov 13 '05 #2

P: n/a


Joona I Palaste wrote:
Varun Sinha <vs****@purdue.edu> 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[i] is 'A')
printf("BlahA");
else if((array[i] is 'B') or (array[i] 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".

--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 13 '05 #3

P: n/a
Al Bowers <xa******@rapidsys.com> wrote:
Joona I Palaste wrote:
Varun Sinha <vs****@purdue.edu> 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[i] is 'A')
printf("BlahA");
else if((array[i] is 'B') or (array[i] 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
Nov 13 '05 #4

P: n/a

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:3f****************@news.nl.net...
Al Bowers <xa******@rapidsys.com> wrote:
Joona I Palaste wrote:
Varun Sinha <vs****@purdue.edu> 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[i] is 'A')
> printf("BlahA");
>else if((array[i] is 'B') or (array[i] 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
Nov 13 '05 #5

P: n/a


Joona I Palaste wrote:
Varun Sinha <vs****@purdue.edu> 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[i] is 'A')
printf("BlahA");
else if((array[i] is 'B') or (array[i] 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[i];
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.

Nov 13 '05 #6

P: n/a


Richard Bos wrote:
Al Bowers <xa******@rapidsys.com> wrote:

Joona I Palaste wrote:

Varun Sinha <vs****@purdue.edu> 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[i] is 'A')
printf("BlahA");
else if((array[i] is 'B') or (array[i] 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.


--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 13 '05 #7

P: n/a
Al Bowers <xa******@rapidsys.com> wrote:


Richard Bos wrote:
Al Bowers <xa******@rapidsys.com> wrote:
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.


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
Nov 13 '05 #8

P: n/a
Thanks for the help!! I got it to work. I was wrong about using the ==
operator with strings.

Thanks to all!!

Varun
Nov 13 '05 #9

P: n/a
vs****@purdue.edu (Varun Sinha) wrote:
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
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.