CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug Reports on this forum. Send them to support@ccsinfo.com

Compare a character of a string

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
nuclear__



Joined: 24 Jan 2015
Posts: 63

View user's profile Send private message

Compare a character of a string
PostPosted: Fri Jan 08, 2021 7:36 am     Reply with quote

I want to compare the first character of a string, with another string, but i always get true(no match).
Here is the way i declare them:
Code:

char usb_buffer[64];
char trig[]="|";

and my compare
Code:

if (strcmp(usb_buffer[0],trig[0]))

i've also tried
Code:
if (strcmp(usb_buffer[0],trig))

and
Code:
if (strncmp(usb_buffer[0],trig[0],1))

and countless others.

Let me mark that this works:
Code:
if (!strcmp(usb_buffer,adcs))


I tried to print usb_buffer[0] to see what is there and i can see characters that i shouldn't (numbers etc).

What is the correct way for my comparison?

Thank you
benoitstjean



Joined: 30 Oct 2007
Posts: 455
Location: Ottawa, Ontario, Canada

View user's profile Send private message

PostPosted: Fri Jan 08, 2021 8:00 am     Reply with quote

You're attempting to compare a single character from that string using a function to compare strings.

If you want to compare the first character, then just do this:

Code:
if( usb_buffer[0] != trig[0] )


As for this: if (!strcmp(usb_buffer,adcs))

Then for sure it will work since you're using the function correctly and you're comparing the strings. You don't need to put the ! since this will give you the opposite result.

Ben
PCM programmer



Joined: 06 Sep 2003
Posts: 21492

View user's profile Send private message

PostPosted: Fri Jan 08, 2021 8:16 am     Reply with quote

benoitstjean wrote:
You don't need to put the ! since this will give you the opposite result.

strcmp() returns 0 (FALSE) if it's a match, so prefixing it with '!' makes it
return 1 (TRUE) if there's a match. Using '!' converts the result to positive
logic, which is how people normally think.
nuclear__



Joined: 24 Jan 2015
Posts: 63

View user's profile Send private message

PostPosted: Fri Jan 08, 2021 8:28 am     Reply with quote

benoitstjean wrote:
You're attempting to compare a single character from that string using a function to compare strings.

If you want to compare the first character, then just do this:

Code:
if( usb_buffer[0] != trig[0] )


As for this: if (!strcmp(usb_buffer,adcs))

Then for sure it will work since you're using the function correctly and you're comparing the strings. You don't need to put the ! since this will give you the opposite result.

Ben


Thank you , that worked!

Code:
memset( usb_buffer, NULL, 64 );

you wrote before was usefull too, i used to use a loop for that, this is much nicer.
Thank you PCM programer, i have it in mind.

One step more . now i want to remove the first character (|) from usb_buffer. How is this possible with a smart way?
i tried shifting << but compiler disagrees ... : Assignment invalid: lvalue is READ ONLY
if no way i have to test a loop shifting one by one ( hoping it works)
Ttelmah



Joined: 11 Mar 2010
Posts: 17398

View user's profile Send private message

PostPosted: Fri Jan 08, 2021 8:43 am     Reply with quote

Shifting is for bits inside a variable. To move a string, you would have to use
strcpy, or memmove. However both would involve a lot of work.
Why move it?. Just don't copy this value out of the buffer.
nuclear__



Joined: 24 Jan 2015
Posts: 63

View user's profile Send private message

PostPosted: Fri Jan 08, 2021 8:56 am     Reply with quote

Ttelmah wrote:
Shifting is for bits inside a variable. To move a string, you would have to use
strcpy, or memmove. However both would involve a lot of work.
Why move it?. Just don't copy this value out of the buffer,


The full story is that if i use | symbol when typing, then the text that follows will be sent to another serial port that a modem listens.
I wanted to have it clean and send it transparently.
Code:
fputs(usb_buffer,SERIAL);

Probably it will be more messy to send bytes usb_buffer[1]...to end rather than correcting it before. At least with my limited knowledge.
Ttelmah



Joined: 11 Mar 2010
Posts: 17398

View user's profile Send private message

PostPosted: Fri Jan 08, 2021 11:03 am     Reply with quote

No, _much_ cleaner.

fputs(&usb_buffer[1],SERIAL);

This involves zero work.

However to use fputs, the data in the buffer needs to have a null terminator.
Is it being sent with such a terminator?.
nuclear__



Joined: 24 Jan 2015
Posts: 63

View user's profile Send private message

PostPosted: Fri Jan 08, 2021 11:46 am     Reply with quote

No i don't add null. After the fputs however, i use putc; command as many times as the characters are to be sent and they leave.
By your sayings i understand that i didn't have to use putc! I will give it a try.
Or it could be the fact that i initialise usb_buffer with /0 before i fill it up, so null is found by fputs... I have many test to perform to understand.

Meanwhile i tried
Code:
for(pointers=0;pointers<(strlen(usb_buffer)+2);pointers++)
            {
               usb_buffer[pointers]=usb_buffer[pointers+1];
            }

and looks working, i will keep the second solution to my notepad!
nuclear__



Joined: 24 Jan 2015
Posts: 63

View user's profile Send private message

PostPosted: Wed Feb 24, 2021 2:04 pm     Reply with quote

Ttelmah wrote:
No, _much_ cleaner.

fputs(&usb_buffer[1],SERIAL);

This involves zero work.

However to use fputs, the data in the buffer needs to have a null terminator.
Is it being sent with such a terminator?.


Hi again. I'm not sure whether i should open a new thread since it's a follow up.
I used this code in this form:
Code:
sprintf(usb_buffer,"%s\r",&ser_phrase[5]);

As you suggested and it works, but now I want to omit the last 2 characters of ser_phrase.
So if it's 10 bytes long (0..9), I want to copy 4 to 7.
Is there any smart way to do that either while executing this sprintf OR removing these 2 last bytes from usb_buffer string after sprintf ?
Ttelmah



Joined: 11 Mar 2010
Posts: 17398

View user's profile Send private message

PostPosted: Thu Feb 25, 2021 2:38 am     Reply with quote

Since the usb_buffer will itself be treated as a string, then:
Code:

sprintf(usb_buffer,"%s\r",&ser_phrase[5]);
usb_buffer[4]='\0';


Should stop the transmission after 4 characters.
nuclear__



Joined: 24 Jan 2015
Posts: 63

View user's profile Send private message

PostPosted: Thu Feb 25, 2021 3:28 am     Reply with quote

Ok. Since string is variable, i will get the length with strln and adjust the command.
Thanks
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group