+ 2
How to check if two characters inside one string are the same? - Answered, thank you!!
How do I do it? Could not find a satisfying answer online... It is driving me insane! Help.
33 odpowiedzi
+ 4
Use floyd's tortoise and hare algorithm. A simple cycle detection algorithm that has one point that traverses as twice as another pointing to nodes in our case the indices of the string, since strings can be indexed just like lists in python or arrays for other programming languages. Since there are going to meet somewhere there is going to be a cycle , find that cycle and we'll have an answer. And the goodness is that it works in 0(n) space and 0(1) time.
+ 3
If you are allowed to alter the original string, then sorting the characters in the string may help you in shortening the time needed in finding duplicate character.
+ 3
Share your code link in your thread Description, let's see what we can find ...
+ 3
If I enter Ituuu it works fine and prints:
TRUE
TRUE
FALSE
+ 3
Or you can replace break with abort() to completely end the programm
and move the cout<<„True“; Statement to after the for loop.
You see there are lots of options to improve this last issue
+ 2
If you use a char* you just can access both and compare them....
+ 2
I need a break first... Will send the code when I‘m back, thank you!
+ 2
First of all, thank you :) And yes, I find it a little difficult because I have an information overload today and can‘t figure out the simplest of tasks as it seems. To check if a char at i == char at i+1 I just need to write sth. like: if (string[i] == string[i+1]? See, little things like char as opposed to string and the way they are used for different things confuse me still.
Or do you mean if (str.at(i) == str.at(i+1)) ??
+ 2
I would suggest you not to print the result in the for loop...
better use the for loop for calculating whether you should print True or false and print it after the for loop
+ 2
You can use std::unique in <algorithm>
string a;
auto e=std::unique(a.begin(),a.end());
e is equal to a.end() if all elements are unique.Otherwise,it points to the first duplicate element
+ 2
Great, thanks!
+ 2
👍 You made it
+ 2
Andrebot AstRein Maybe you mark your question as solved if you want to avoid more and more answers like from Noman and Ameer Haider, who post after your code works...
+ 1
Will try :) Thank you.
+ 1
An example?
Does letter case matter?
Alphabet only or any character?
+ 1
It is the Isogram challenge. I need to check if any character in a string repeats. E.g. bool is not an isogram but hour is.
+ 1
You are welcome
+ 1
I tried that, sorted the string, created a nested loop but still couldn‘t get what I wanted... frustrating! I‘m still trying right now, using the str.find method.
+ 1
Ok, so I sorted a string to , let‘s say, “eHllo”. Now I want to check if any character occurs twice in the string. I think I am making things too complicated... Do I really have to use a loop for this task? Is there a way around it using pointers somehow??