473,396 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Comparing elements of an array to a char

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
9 8088
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


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
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

"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


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


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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: agent349 | last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1 == array2)). However, I've been trying to compare two arrays using pointers with no success. Basically, I want to take three...
5
by: ma740988 | last post by:
There's a need for me to move around at specified offsets within memory. As as a result - long story short - unsigned char* is the type of choice. At issue: Consider the case ( test code ) where...
1
by: psmahesh | last post by:
Hi folks, I am comparing two arrays and removing matches from the second array from the first array. Can someone take a look at this code below and mention if this is okay and perhaps if there...
20
by: Bill Pursell | last post by:
This question involves code relying on mmap, and thus is not maximally portable. Undoubtedly, many will complain that my question is not topical... I have two pointers, the first of which is...
25
by: galapogos | last post by:
Hi, I'm trying to compare an array of unsigned chars(basically just data without any context) with a constant, and I'm not sure how to do that. Say my array is array and I want to compare it with...
4
by: =?Utf-8?B?cm9nZXJfMjc=?= | last post by:
hey, I have a method that takes a char array of 10. I have a char array of 30. how do I make it send the first 10, then the next 10, then the final 10 ? I need help with my looping skills....
25
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void...
7
by: arnuld | last post by:
On Mon, 28 Apr 2008 07:25:53 +0000, Richard Heathfield wrote: Either you are not understanding me or I am not able to understand this <p_strcmpthing. This is your code:
2
by: TamaThps | last post by:
I have to write a program that takes the lines of code from a .cpp as a string into an array. Then I need to compute the ratio of total lines of code to the number of comment lines, and ratio of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.