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

C: compare character

P: n/a
hi,
I want to compare character, if the string contains character "-" then it
will print Hello on the screen
however, neither method (1) nor method (2) work in the code below:
so what the correct code should be? thanks!

#include <stdio.h>
#include <stdlib.h>

int main()
{
int i;
char *str;

*str = "abc - xyz";
for (i=0; i<35; i++){

/* method (1) does not work ! */
if (strcmp(str[i], "-") == 0) printf("Hello1");

/* method (2) shows nth ! */
if (str[i] == "-") printf("Hello2");
}

system("PAUSE");
return 0;
}

Nov 14 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Sharon wrote:
hi,
I want to compare character, if the string contains character "-" then it
will print Hello on the screen
however, neither method (1) nor method (2) work in the code below:
so what the correct code should be? thanks!

#include <stdio.h>
#include <stdlib.h>

int main()
{
int i;
char *str;

*str = "abc - xyz";
Well, here's the first problem: You just dereferenced an uninitialized
pointer. Try replacing those last two lines with this:

char const* str = "abc - xyz";
for (i=0; i<35; i++){


/* method (1) does not work ! */
if (strcmp(str[i], "-") == 0) printf("Hello1");


str[1] is a character. strcmp only knows how to compare "strings" of
characters.
/* method (2) shows nth ! */
if (str[i] == "-") printf("Hello2");
You're comparing a single character ( str[i] ) to a string ( "-" ). I
can certainly understand why you might think this would work; it would
be very intuitive. Unfortunately, it doesn't work that way.

Try method (3):

if( str[i] == '-' ) printf( "Hello3\n" );
}

system("PAUSE");
return 0;
}


Good luck!

-Jeff

Nov 14 '05 #2

P: n/a
"Sharon" <sh************@hotmail.com> wrote in message
news:bs**********@news.hgc.com.hk...
hi,
I want to compare character, if the string contains character "-" then it
will print Hello on the screen
however, neither method (1) nor method (2) work in the code below:
so what the correct code should be? thanks!
Doesn't even compile ( VC++ 6.0 sp5 )
#include <stdio.h>
#include <stdlib.h>

int main()
{
int i;
char *str;
str is pointer to a character, right?
*str = "abc - xyz";
So, what is *str now?
for (i=0; i<35; i++){
The 35 here, why?
/* method (1) does not work ! */
if (strcmp(str[i], "-") == 0) printf("Hello1");
Oh, we have an array now?
Exactly which address in memory are we looking at?
Probably a good thing we didn't assign to it!
( Danger Will Robinson! )
/* method (2) shows nth ! */
if (str[i] == "-") printf("Hello2");
Ditto.
}

system("PAUSE");
return 0;
}

--

Cheers
--
Hewson::Mike
"This letter is longer than usual because I lack the time to make it
shorter" - Blaise Pascal
Nov 14 '05 #3

P: n/a
Sharon wrote:
hi,
I want to compare character, if the string contains character "-" then it
will print Hello on the screen
however, neither method (1) nor method (2) work in the code below:
so what the correct code should be? thanks!

#include <stdio.h>
#include <stdlib.h>

int main()
{
int i;
char *str;

*str = "abc - xyz";
for (i=0; i<35; i++){
By the way, why are you iterating 35 times? Your string is not that long.

/* method (1) does not work ! */
if (strcmp(str[i], "-") == 0) printf("Hello1");

/* method (2) shows nth ! */
if (str[i] == "-") printf("Hello2");
}

system("PAUSE");
return 0;
}


Nov 14 '05 #4

P: n/a
In article <bs**********@news.hgc.com.hk>, sh************@hotmail.com
says...
hi,
I want to compare character, if the string contains character "-" then it
will print Hello on the screen


Look up 'strchr' in your favorite reference.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Nov 14 '05 #5

P: n/a
With credits to Jeff Schwab, below should be a working program:

#include <stdio.h>
#include <stdlib.h>

int main()
{
int i=0, j=0;
char *str = "abc - xy-zdsd sdsd -x-y-";

while(str[i])
if (str[i++] == '-') printf("Found %d minus\n", ++j);

system("PAUSE");
return 0;
}

"Jeff Schwab" <je******@comcast.net> wrote in message
news:R8********************@comcast.com...
Sharon wrote:
hi,
I want to compare character, if the string contains character "-" then it will print Hello on the screen
however, neither method (1) nor method (2) work in the code below:
so what the correct code should be? thanks!

#include <stdio.h>
#include <stdlib.h>

int main()
{
int i;
char *str;

*str = "abc - xyz";
for (i=0; i<35; i++){


By the way, why are you iterating 35 times? Your string is not that long.

/* method (1) does not work ! */
if (strcmp(str[i], "-") == 0) printf("Hello1");

/* method (2) shows nth ! */
if (str[i] == "-") printf("Hello2");
}

system("PAUSE");
return 0;
}


Nov 14 '05 #6

P: n/a
Sharon wrote:
hi,
I want to compare character, if the string contains character "-" then it
will print Hello on the screen
however, neither method (1) nor method (2) work in the code below:
so what the correct code should be? thanks!
It'll be worth rooting through your compiler docs to find the options
that'll make it "more fussy". I've inserted the complaints from mine into
your source.
#include <stdio.h>
#include <stdlib.h>

int main()
{
int i;
char *str;

*str = "abc - xyz"; ^^^
(9)
for (i=0; i<35; i++){

/* method (1) does not work ! */
if (strcmp(str[i], "-") == 0) printf("Hello1"); ^^^
(13)
/* method (2) shows nth ! */
if (str[i] == "-") printf("Hello2"); ^^^
(16) }

system("PAUSE");
return 0;
}
Your errors will differ. You'll get used to what they mean over time...

c.c: In function `main':
c.c:9: warning: assignment makes integer from pointer without a cast
^^^
I'll come back to this.

c.c:13: warning: implicit declaration of function `strcmp'
^^^
Tells me I've forgotten the header. strcmp() lives in <string.h>

c.c:13: warning: passing arg 1 of `strcmp' makes pointer from integer
without a cast
^^^
Mixing pointers and integers is almost always "a bad thing". Provided we've
included the header for it, it often means the arguments are wrong.

c.c:16: warning: comparison between pointer and integer
^^^
Ditto.

This should compile. It's still not in the "works" category though...

#include <stdio.h>
#include <stdlib.h>
#include <string.h> /* strcmp */

int main()
{
int i;
const char str[]="abc - xyz"; /* a non-modifable string */

for (i=0; i<35; i++){

/* takes two string (const char*) arguments */
if (strcmp(str,"-") == 0) printf("Hello1");

/* compare a char with a char (single quotes) */
if (str[i] == '-') printf("Hello2");
}

/* no PAUSE command here so I deleted it */

return 0;
}

The major problem above is the loop. It's fixed at 35 characters but 'str'
is a lot shorter than that. Add a (size_t) variable to hold the result of
strlen() then use that value in the loop. Hint...

/* code */
const size_t len = strlen(str);
for (i = 0; i < len; i++) {
/* rest of code */
}

You'll note that the strcmp() decision will never occur unless 'str' is
exactly "-". Obviously it's not an exact match you're after so strcmp()
can't help here. There are a few search-like functions - probably the best
one for this is strchr(). It searches a string for the first (leftmost)
occurrence of a character and returns a pointer to it (if found) or NULL if
there isn't one.

#include <stdio.h>
#include <string.h>

int main()
{const char str[] ="abc - xyz";
const char *found;

found = strchr(str,'-');
if (NULL != found)
printf("got it\n");
else
printf("nope\n")
;
return 0;
}

The problem with your 'str'...
char *str;
*str = "abc - xyz";


....was that [char *str] just declared a pointer. There was no actual string
associated with it. This would have worked because it makes the 'str'
pointer point at the string literal "abc - xyz"...

char *str;
str = "abc - xyz";

....except for the fact string literals cannot be modified. You can often get
away with the above for historic reasons (old K&R C compilers were crap)
but in actual fact it should be...

const char *str;
str = "abc - xyz";

....the difference being that now the compiler will realise if you attempt to
modify "abc - xyz" with (eg str[0]='A') whereas it wouldn't realise without
the 'const' - it would really try to change "abc - xyz" into "Abc - xyz".
The effects of doing so range from it actually succeeding (almost
impossible to debug) to an instant crash (easy to find).
--
Guy Harrison
Nov 14 '05 #7

P: n/a
Lance wrote:
With credits to Jeff Schwab, below should be a working program:

#include <stdio.h>
#include <stdlib.h>

int main()
{
int i=0, j=0;
char *str = "abc - xy-zdsd sdsd -x-y-";

while(str[i])
if (str[i++] == '-') printf("Found %d minus\n", ++j);

system("PAUSE");
return 0;
}


As it's nearly Crimbo... ;-)

printf("%s\n",strchr("abc - xyz",'-')?"yey":"ney");
--
Guy Harrison
Nov 14 '05 #8

P: n/a
Sharon wrote:
hi,
I want to compare character, if the string contains character "-" then it
will print Hello on the screen
however, neither method (1) nor method (2) work in the code below:
so what the correct code should be? thanks!


#include <string.h>
#include <stdio.h>

void checkforhyphen(const char *s)
{
if (strchr(s,'-')) puts("Hello");
/* printf("Hello") is closer to your spec, actually */
}
--
Martin Ambuhl

Nov 14 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.