Question:
C Programming Comparing a string?
PhysicsLeet
2011-09-23 11:28:31 UTC
I am currently coding a application. I reading a user input using fgets() and then comparing it to a string which i retrieve from a mysql database. my problem is that when i read the user input i initially have to create a char[] array with a definite size. after the two strings are determined i try to compare them but they wont equal because the char[] will have empty characters after it. How would i get around this?
Five answers:
McFate
2011-09-23 11:32:22 UTC
In C, strings are delimited by a null at the end of the String.



The standard C library operation strcmp() will compare two strings, stopping a the null character which terminates it. The remaining characters in the buffer wouldn't be looked at.
?
2011-09-23 11:35:12 UTC
Whenever you manipulate strings, you will always have the possibility of white space, whether the string is part of a char array or you are just iterating through a string.



If you must use a char array, loop through each char and trim off the white space. Once you have massaged the char, you can then compare the values as needed.



It it were me, I would just cycle through the string with a function call to "substr" and then read each character one by one. This is the same as setting up a char array but is easier.
?
2016-10-22 09:30:34 UTC
What does it recommend to get the ASCII equivalent? Characters are kept(on maximum implementations) as ASCII (or utf-8, of which ASCII is a subset), so there is not any longer something to 'get', it truly is already there. you may evaluate characters promptly, in a for loop. be particular to attempt for the null terminator('0' or in simple terms 0) to attraction to close even as the string is ended.
jplatt39
2011-09-23 11:43:39 UTC
If you aren't using strcmp() why not? If you aren't supposed to then clone it. Read until you come to the NULL character.
anonymous
2011-09-23 11:33:42 UTC
if they are both null terminated stinrgs and are the same you can use strcmp


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...