C: compare character

S

Sharon

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, "-") == 0) printf("Hello1");

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

system("PAUSE");
return 0;
}
 
J

Jeff Schwab

Sharon said:
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, "-") == 0) printf("Hello1");


str[1] is a character. strcmp only knows how to compare "strings" of
characters.
/* method (2) shows nth ! */
if (str == "-") printf("Hello2");


You're comparing a single character ( str ) 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 == '-' ) printf( "Hello3\n" );
}

system("PAUSE");
return 0;
}

Good luck!

-Jeff
 
J

Jeff Schwab

Sharon said:
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, "-") == 0) printf("Hello1");

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

system("PAUSE");
return 0;
}
 
M

Mike Hewson

Sharon said:
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, "-") == 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 == "-") printf("Hello2");
Ditto.

}

system("PAUSE");
return 0;
}
 
J

Jerry Coffin

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

Lance

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)
if (str[i++] == '-') printf("Found %d minus\n", ++j);

system("PAUSE");
return 0;
}

Jeff Schwab said:
Sharon said:
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, "-") == 0) printf("Hello1");

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

system("PAUSE");
return 0;
}

 
G

Guy Harrison

Sharon said:
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, "-") == 0) printf("Hello1"); ^^^
(13)

/* method (2) shows nth ! */
if (str == "-") 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 == '-') 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).
 
G

Guy Harrison

Lance said:
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)
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");
 
M

Martin Ambuhl

Sharon said:
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 */
}
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top