Palindrome algorithm
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
-1
down vote
favorite
This code could be better?
This is the algorithm:
- Compare the 1st character to the last character
- Compare the 2nd character to the second last character and so on
- Stop when the middle of the string is reached
- Just words
namespace Palindrome
class Class1
static void Main(string args)
while (true)
Console.WriteLine("Digit a word:");
string word = Console.ReadLine();
if (word == "exit")
break;
VerifyPalindrome(word);
private static void VerifyPalindrome(string word)
bool compare = true;
int i = 0;
while (i < word.Length- 1)
if (compare)
for (int j = word.Length - 1; j >= 0; j--)
if (word[i] == word[j])
compare = true;
else
compare = false;
break;
i++;
else
break;
if (compare)
Console.WriteLine("This is a Palindrome word");
else
Console.WriteLine("This is NOT a Palindrome word");
c# algorithm palindrome
add a comment |Â
up vote
-1
down vote
favorite
This code could be better?
This is the algorithm:
- Compare the 1st character to the last character
- Compare the 2nd character to the second last character and so on
- Stop when the middle of the string is reached
- Just words
namespace Palindrome
class Class1
static void Main(string args)
while (true)
Console.WriteLine("Digit a word:");
string word = Console.ReadLine();
if (word == "exit")
break;
VerifyPalindrome(word);
private static void VerifyPalindrome(string word)
bool compare = true;
int i = 0;
while (i < word.Length- 1)
if (compare)
for (int j = word.Length - 1; j >= 0; j--)
if (word[i] == word[j])
compare = true;
else
compare = false;
break;
i++;
else
break;
if (compare)
Console.WriteLine("This is a Palindrome word");
else
Console.WriteLine("This is NOT a Palindrome word");
c# algorithm palindrome
2
Your code claims thatfoo
andba
are palindromes.
â Marvin
Feb 24 at 10:41
The problem is on while clausule:while (i < (word.Length / 2) - 1)
Thanks
â Pankwood
Feb 24 at 16:10
I've fixed the code.
â Pankwood
Mar 1 at 1:25
add a comment |Â
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
This code could be better?
This is the algorithm:
- Compare the 1st character to the last character
- Compare the 2nd character to the second last character and so on
- Stop when the middle of the string is reached
- Just words
namespace Palindrome
class Class1
static void Main(string args)
while (true)
Console.WriteLine("Digit a word:");
string word = Console.ReadLine();
if (word == "exit")
break;
VerifyPalindrome(word);
private static void VerifyPalindrome(string word)
bool compare = true;
int i = 0;
while (i < word.Length- 1)
if (compare)
for (int j = word.Length - 1; j >= 0; j--)
if (word[i] == word[j])
compare = true;
else
compare = false;
break;
i++;
else
break;
if (compare)
Console.WriteLine("This is a Palindrome word");
else
Console.WriteLine("This is NOT a Palindrome word");
c# algorithm palindrome
This code could be better?
This is the algorithm:
- Compare the 1st character to the last character
- Compare the 2nd character to the second last character and so on
- Stop when the middle of the string is reached
- Just words
namespace Palindrome
class Class1
static void Main(string args)
while (true)
Console.WriteLine("Digit a word:");
string word = Console.ReadLine();
if (word == "exit")
break;
VerifyPalindrome(word);
private static void VerifyPalindrome(string word)
bool compare = true;
int i = 0;
while (i < word.Length- 1)
if (compare)
for (int j = word.Length - 1; j >= 0; j--)
if (word[i] == word[j])
compare = true;
else
compare = false;
break;
i++;
else
break;
if (compare)
Console.WriteLine("This is a Palindrome word");
else
Console.WriteLine("This is NOT a Palindrome word");
c# algorithm palindrome
edited Mar 1 at 1:24
asked Feb 23 at 22:30
Pankwood
1014
1014
2
Your code claims thatfoo
andba
are palindromes.
â Marvin
Feb 24 at 10:41
The problem is on while clausule:while (i < (word.Length / 2) - 1)
Thanks
â Pankwood
Feb 24 at 16:10
I've fixed the code.
â Pankwood
Mar 1 at 1:25
add a comment |Â
2
Your code claims thatfoo
andba
are palindromes.
â Marvin
Feb 24 at 10:41
The problem is on while clausule:while (i < (word.Length / 2) - 1)
Thanks
â Pankwood
Feb 24 at 16:10
I've fixed the code.
â Pankwood
Mar 1 at 1:25
2
2
Your code claims that
foo
and ba
are palindromes.â Marvin
Feb 24 at 10:41
Your code claims that
foo
and ba
are palindromes.â Marvin
Feb 24 at 10:41
The problem is on while clausule:
while (i < (word.Length / 2) - 1)
Thanksâ Pankwood
Feb 24 at 16:10
The problem is on while clausule:
while (i < (word.Length / 2) - 1)
Thanksâ Pankwood
Feb 24 at 16:10
I've fixed the code.
â Pankwood
Mar 1 at 1:25
I've fixed the code.
â Pankwood
Mar 1 at 1:25
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
As already stated you don't need to set it to true multiple times.
There is cleaner syntax for this.
static bool IsPalidrone(string word)
for (int i = 0, j = word.Length - 1; i < j; i++, j--)
if (word[i] != word[j])
return false;
return true;
Clean and smart! Thanks!
â Pankwood
Feb 24 at 18:24
@DaniloDebiaziVicente You could give it the check if it is the answer.
â paparazzo
Feb 24 at 18:31
I have less than 15 reputation points here. Unfortunately, the Stack Exchange doesn't allow me to check your answer. I'll do that when I got my 15 points.
â Pankwood
Feb 26 at 1:04
@DaniloDebiaziVicente Up vote and check are not the same. But no problem.
â paparazzo
Feb 26 at 6:33
@Pararazzi I got it!
â Pankwood
Feb 27 at 22:48
add a comment |Â
up vote
2
down vote
One thing I would like to change is the responsibilities of VerifyPalindrome(string word)
. Remember, your function should only check whether word
is a palindrome, nothing else. It shouldn't print out a value, but instead return a boolean that verifies wheter the word is a palindrome or not. Change the function to:
private static bool VerifyPalindrome(string word)
bool compare = true;
// ... computing code
return compare;
And in Main
:
if(VerifyPalindrome(word))
Console.WriteLine("This is a Palindrome word");
else
Console.WriteLine("This is NOT a Palindrome word");
The other thing I would change is your palindrome logic. Instead of keeping track of a variable compare
for whether a string is a palindrome or not and using break
s, which increases the complexity of your code, you can reduce the function to the following, with multiple return
s:
private static bool VerifyPalindrome(string word)
int i = 0;
while (i < (word.Length / 2) - 1)
for (int j = word.Length - 1; j >= 0; j--)
if (word[i] != word[j])
return false;
i++;
return true;
No you could further shorten this by combining the two loops into one, but I'll leave that as an exercise to you.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
As already stated you don't need to set it to true multiple times.
There is cleaner syntax for this.
static bool IsPalidrone(string word)
for (int i = 0, j = word.Length - 1; i < j; i++, j--)
if (word[i] != word[j])
return false;
return true;
Clean and smart! Thanks!
â Pankwood
Feb 24 at 18:24
@DaniloDebiaziVicente You could give it the check if it is the answer.
â paparazzo
Feb 24 at 18:31
I have less than 15 reputation points here. Unfortunately, the Stack Exchange doesn't allow me to check your answer. I'll do that when I got my 15 points.
â Pankwood
Feb 26 at 1:04
@DaniloDebiaziVicente Up vote and check are not the same. But no problem.
â paparazzo
Feb 26 at 6:33
@Pararazzi I got it!
â Pankwood
Feb 27 at 22:48
add a comment |Â
up vote
3
down vote
accepted
As already stated you don't need to set it to true multiple times.
There is cleaner syntax for this.
static bool IsPalidrone(string word)
for (int i = 0, j = word.Length - 1; i < j; i++, j--)
if (word[i] != word[j])
return false;
return true;
Clean and smart! Thanks!
â Pankwood
Feb 24 at 18:24
@DaniloDebiaziVicente You could give it the check if it is the answer.
â paparazzo
Feb 24 at 18:31
I have less than 15 reputation points here. Unfortunately, the Stack Exchange doesn't allow me to check your answer. I'll do that when I got my 15 points.
â Pankwood
Feb 26 at 1:04
@DaniloDebiaziVicente Up vote and check are not the same. But no problem.
â paparazzo
Feb 26 at 6:33
@Pararazzi I got it!
â Pankwood
Feb 27 at 22:48
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
As already stated you don't need to set it to true multiple times.
There is cleaner syntax for this.
static bool IsPalidrone(string word)
for (int i = 0, j = word.Length - 1; i < j; i++, j--)
if (word[i] != word[j])
return false;
return true;
As already stated you don't need to set it to true multiple times.
There is cleaner syntax for this.
static bool IsPalidrone(string word)
for (int i = 0, j = word.Length - 1; i < j; i++, j--)
if (word[i] != word[j])
return false;
return true;
answered Feb 23 at 23:45
paparazzo
4,8131730
4,8131730
Clean and smart! Thanks!
â Pankwood
Feb 24 at 18:24
@DaniloDebiaziVicente You could give it the check if it is the answer.
â paparazzo
Feb 24 at 18:31
I have less than 15 reputation points here. Unfortunately, the Stack Exchange doesn't allow me to check your answer. I'll do that when I got my 15 points.
â Pankwood
Feb 26 at 1:04
@DaniloDebiaziVicente Up vote and check are not the same. But no problem.
â paparazzo
Feb 26 at 6:33
@Pararazzi I got it!
â Pankwood
Feb 27 at 22:48
add a comment |Â
Clean and smart! Thanks!
â Pankwood
Feb 24 at 18:24
@DaniloDebiaziVicente You could give it the check if it is the answer.
â paparazzo
Feb 24 at 18:31
I have less than 15 reputation points here. Unfortunately, the Stack Exchange doesn't allow me to check your answer. I'll do that when I got my 15 points.
â Pankwood
Feb 26 at 1:04
@DaniloDebiaziVicente Up vote and check are not the same. But no problem.
â paparazzo
Feb 26 at 6:33
@Pararazzi I got it!
â Pankwood
Feb 27 at 22:48
Clean and smart! Thanks!
â Pankwood
Feb 24 at 18:24
Clean and smart! Thanks!
â Pankwood
Feb 24 at 18:24
@DaniloDebiaziVicente You could give it the check if it is the answer.
â paparazzo
Feb 24 at 18:31
@DaniloDebiaziVicente You could give it the check if it is the answer.
â paparazzo
Feb 24 at 18:31
I have less than 15 reputation points here. Unfortunately, the Stack Exchange doesn't allow me to check your answer. I'll do that when I got my 15 points.
â Pankwood
Feb 26 at 1:04
I have less than 15 reputation points here. Unfortunately, the Stack Exchange doesn't allow me to check your answer. I'll do that when I got my 15 points.
â Pankwood
Feb 26 at 1:04
@DaniloDebiaziVicente Up vote and check are not the same. But no problem.
â paparazzo
Feb 26 at 6:33
@DaniloDebiaziVicente Up vote and check are not the same. But no problem.
â paparazzo
Feb 26 at 6:33
@Pararazzi I got it!
â Pankwood
Feb 27 at 22:48
@Pararazzi I got it!
â Pankwood
Feb 27 at 22:48
add a comment |Â
up vote
2
down vote
One thing I would like to change is the responsibilities of VerifyPalindrome(string word)
. Remember, your function should only check whether word
is a palindrome, nothing else. It shouldn't print out a value, but instead return a boolean that verifies wheter the word is a palindrome or not. Change the function to:
private static bool VerifyPalindrome(string word)
bool compare = true;
// ... computing code
return compare;
And in Main
:
if(VerifyPalindrome(word))
Console.WriteLine("This is a Palindrome word");
else
Console.WriteLine("This is NOT a Palindrome word");
The other thing I would change is your palindrome logic. Instead of keeping track of a variable compare
for whether a string is a palindrome or not and using break
s, which increases the complexity of your code, you can reduce the function to the following, with multiple return
s:
private static bool VerifyPalindrome(string word)
int i = 0;
while (i < (word.Length / 2) - 1)
for (int j = word.Length - 1; j >= 0; j--)
if (word[i] != word[j])
return false;
i++;
return true;
No you could further shorten this by combining the two loops into one, but I'll leave that as an exercise to you.
add a comment |Â
up vote
2
down vote
One thing I would like to change is the responsibilities of VerifyPalindrome(string word)
. Remember, your function should only check whether word
is a palindrome, nothing else. It shouldn't print out a value, but instead return a boolean that verifies wheter the word is a palindrome or not. Change the function to:
private static bool VerifyPalindrome(string word)
bool compare = true;
// ... computing code
return compare;
And in Main
:
if(VerifyPalindrome(word))
Console.WriteLine("This is a Palindrome word");
else
Console.WriteLine("This is NOT a Palindrome word");
The other thing I would change is your palindrome logic. Instead of keeping track of a variable compare
for whether a string is a palindrome or not and using break
s, which increases the complexity of your code, you can reduce the function to the following, with multiple return
s:
private static bool VerifyPalindrome(string word)
int i = 0;
while (i < (word.Length / 2) - 1)
for (int j = word.Length - 1; j >= 0; j--)
if (word[i] != word[j])
return false;
i++;
return true;
No you could further shorten this by combining the two loops into one, but I'll leave that as an exercise to you.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
One thing I would like to change is the responsibilities of VerifyPalindrome(string word)
. Remember, your function should only check whether word
is a palindrome, nothing else. It shouldn't print out a value, but instead return a boolean that verifies wheter the word is a palindrome or not. Change the function to:
private static bool VerifyPalindrome(string word)
bool compare = true;
// ... computing code
return compare;
And in Main
:
if(VerifyPalindrome(word))
Console.WriteLine("This is a Palindrome word");
else
Console.WriteLine("This is NOT a Palindrome word");
The other thing I would change is your palindrome logic. Instead of keeping track of a variable compare
for whether a string is a palindrome or not and using break
s, which increases the complexity of your code, you can reduce the function to the following, with multiple return
s:
private static bool VerifyPalindrome(string word)
int i = 0;
while (i < (word.Length / 2) - 1)
for (int j = word.Length - 1; j >= 0; j--)
if (word[i] != word[j])
return false;
i++;
return true;
No you could further shorten this by combining the two loops into one, but I'll leave that as an exercise to you.
One thing I would like to change is the responsibilities of VerifyPalindrome(string word)
. Remember, your function should only check whether word
is a palindrome, nothing else. It shouldn't print out a value, but instead return a boolean that verifies wheter the word is a palindrome or not. Change the function to:
private static bool VerifyPalindrome(string word)
bool compare = true;
// ... computing code
return compare;
And in Main
:
if(VerifyPalindrome(word))
Console.WriteLine("This is a Palindrome word");
else
Console.WriteLine("This is NOT a Palindrome word");
The other thing I would change is your palindrome logic. Instead of keeping track of a variable compare
for whether a string is a palindrome or not and using break
s, which increases the complexity of your code, you can reduce the function to the following, with multiple return
s:
private static bool VerifyPalindrome(string word)
int i = 0;
while (i < (word.Length / 2) - 1)
for (int j = word.Length - 1; j >= 0; j--)
if (word[i] != word[j])
return false;
i++;
return true;
No you could further shorten this by combining the two loops into one, but I'll leave that as an exercise to you.
edited Feb 24 at 14:24
answered Feb 23 at 23:20
Arnav Borborah
654119
654119
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f188234%2fpalindrome-algorithm%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
2
Your code claims that
foo
andba
are palindromes.â Marvin
Feb 24 at 10:41
The problem is on while clausule:
while (i < (word.Length / 2) - 1)
Thanksâ Pankwood
Feb 24 at 16:10
I've fixed the code.
â Pankwood
Mar 1 at 1:25