finding if string has unique characters

Mar 28, 2013 at 10:57am
bool isUnique(string _str)
{
bool char_set[256];
int len = _str.length();

memset(char_set, '\0', 256);
for(int i = 0; i < len; ++i)
{
int val = _str[i]- '0';
if(char_set[val])
{
return false;
}
char_set[val] = true;
}

return true;
}

Hi....I came across this code to find if string has unique characters...i didnt understand why they subracted ascii value of character '0' in the statement int val = _str[i]- '0' and what is happening with the statements...if(char_set[val])
{
return false;
}
char_set[val] = true;


i take each character in the sting and traverse the whole string .and if count is 2 i use break and conclude that its not unique and not otherwise...can i use this method or this is not efficient????

please help....i am new to c++...thanks..
Mar 28, 2013 at 11:24am
This is just trying to create an index value for each printable character.

lets say _str has "hello world" in it.
int val =_str[0] - '0' will give the ascii value of the first letter;
this will convert '0' to its ascii value 48 (is it 47 ?) and h to 104. So you will get 104 - 47 = 57;

similarly, for each character a unique value will be generated and then that index in char_set will be set to true;
in case that index is again generated (this will happen if that character will come again), then that index in char_set will already be true and it will be assumed that the character has already been seen and its not a unique string.
Mar 28, 2013 at 12:21pm
thanks @ writetonsharma (1243)

cant i work it out through alternative method like this...... i take each character in the sting and traverse the whole string .and if count is 2 i use break and conclude that its not unique and not otherwise...can i go this method or this is not efficient????
Mar 28, 2013 at 12:27pm
using this statement int val =_str[0] - '0' what if one of the characters in the string has ascii value less than 48 and val turns into negative....then particular character will not get any place in char_set for its boolean......why cant we leave it like this...int value=str[0]????

thanks
Mar 28, 2013 at 2:18pm
> int val =_str[0] - '0' what if one of the characters in the string has ascii value less than 48 '0' and val turns into negative

Undefined behaviour.


> why cant we leave it like this...int value=str[0]????

What if std::numeric_limits<char>::is_signed is true and one of the characters in the string has a negative value?
Last edited on Mar 28, 2013 at 2:18pm
Mar 28, 2013 at 2:39pm
We are talking about printable characters or only numeric/alphabets. So, nothing like -ve will come.. because anything below ascii 48 will not be in the input. obviously you can change the code to include non-printable characters too.

You can do any method you like and which is easy to understand. the idea is to learn programming and not write optimal code at the moment.
Mar 28, 2013 at 2:57pm
> We are talking about printable characters or only numeric/alphabets.
> So, nothing like -ve will come.
> because anything below ascii 48 will not be in the input.

Don't be asinine. You have no idea of what you are talking about.


@venkatacplpl - You have the right idea; now try to work out a correct way of doing this.
Mar 28, 2013 at 6:48pm
This is what the program is suppose to do (or written).. not taking special characters and others... obviously there are printable characters below 48..

Mar 28, 2013 at 8:03pm
everyone....thanks for replying.
Topic archived. No new replies allowed.