6 Replies - 343 Views - Last Post: 02 September 2013 - 11:51 AM Rate Topic: -----

#1 sundet  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-September 13

Comparison of Original and Reversed String; Palindrome Checker

Posted 01 September 2013 - 06:36 PM

Good evening,

I need some assistance with a program I wrote which asks for input of a string, reverses the string and compares the reversed string to the original string. When I output the reversed string to the screen, it looks right. However, when the length function is run, the length is only one so if I type a single character when prompted, the result is a match. Why does my program only work as I want it to work when I type in one character?

Below is my code. My guess is that there are better ways to achieve what I want to do. However, I want to learn why my program isn't working as I want it to work. I hope that somebody is willing to help me out.

int main()

{int LenPhrase=0;
int i=0;
string ReversedPhrase="";
ReverseIt phrase;
cout<<"Enter a phrase:";
getline(cin,phrase.word);
LenPhrase=phrase.word.length();

for(i=LenPhrase-1;i>=1;i--)
	cout<<phrase.word[i];
{ReversedPhrase=ReversedPhrase+phrase.word[i];
}

cout<<ReversedPhrase;

cout<<endl;


if(phrase.word==ReversedPhrase)
{cout<<"There is a match.";}


return 0;}


Is This A Good Question/Topic? 0
  • +

Replies To: Comparison of Original and Reversed String; Palindrome Checker

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 3991
  • View blog
  • Posts: 12,317
  • Joined: 25-December 09

Re: Comparison of Original and Reversed String; Palindrome Checker

Posted 01 September 2013 - 06:54 PM

My first suggestion is that you find an indentation style and use it consistently. This will make your program readable to us mere humans.

So please post properly formatted code so we can read it.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Comparison of Original and Reversed String; Palindrome Checker

Posted 01 September 2013 - 07:13 PM

First, welcome to the d.i.c forum!

Let's gussy up your code. After awhile, our brains get wired to recognize standard C idiom's when they are in a standard indentation format, MUCH easier, than without it.

int main()
{
   int LenPhrase=0;
   int i=0;
   string ReversedPhrase="";
   ReverseIt phrase;
   cout<<"Enter a phrase:";

   getline(cin,phrase.word);
   LenPhrase=phrase.word.length();

   for(i=LenPhrase-1;i>=1;i--)
      cout<<phrase.word[i];
   {  
      ReversedPhrase=ReversedPhrase+phrase.word[i];
   }

   cout<<ReversedPhrase;
   cout<<endl;


   if(phrase.word==ReversedPhrase)
   {
      cout<<"There is a match.";
   }
   return 0;
}



There, that's better! So first thing, are you getting warnings when you compile your program? Best to treat most warnings as things to be fixed, for now.

I haven't run your code, but this immediately caught my eye:
   for(i=LenPhrase-1;i>=1;i--)
      cout<<phrase.word[i];

   {  //<---- Looks out of place (two low).
      ReversedPhrase=ReversedPhrase+phrase.word[i];
   }



What do you think?
Was This Post Helpful? 0
  • +
  • -

#4 sundet  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-September 13

Re: Comparison of Original and Reversed String; Palindrome Checker

Posted 02 September 2013 - 09:35 AM

View PostAdak, on 01 September 2013 - 07:13 PM, said:

First, welcome to the d.i.c forum!

Let's gussy up your code. After awhile, our brains get wired to recognize standard C idiom's when they are in a standard indentation format, MUCH easier, than without it.

int main()
{
   int LenPhrase=0;
   int i=0;
   string ReversedPhrase="";
   ReverseIt phrase;
   cout<<"Enter a phrase:";

   getline(cin,phrase.word);
   LenPhrase=phrase.word.length();

   for(i=LenPhrase-1;i>=1;i--)
      cout<<phrase.word[i];
   {  
      ReversedPhrase=ReversedPhrase+phrase.word[i];
   }

   cout<<ReversedPhrase;
   cout<<endl;


   if(phrase.word==ReversedPhrase)
   {
      cout<<"There is a match.";
   }
   return 0;
}



There, that's better! So first thing, are you getting warnings when you compile your program? Best to treat most warnings as things to be fixed, for now.

I haven't run your code, but this immediately caught my eye:
   for(i=LenPhrase-1;i>=1;i--)
      cout<<phrase.word[i];

   {  //<---- Looks out of place (two low).
      ReversedPhrase=ReversedPhrase+phrase.word[i];
   }



What do you think?

Thanks for improving on the spacing of the code. I don't get any warnings when I compile the program. I'm using Visual Studio 2010.

I don't think that i starts out too low. If I take out the -1, I get a subscript out of range error; no warnings at compile time. I didn't mention before that when I try to output with an index of 1 or 2 for ReversedPhrase[x], the program compiles without warnings but I get a subscript out of range error. I would look for "/0" but I don't think that I can given what I previously mentioned. I added the append function to ReversedPhrase (ReversedPhrase.append("aaa")) after the for loop. There was no output for that line. However, when the same was done in the loop with the input of "bob", I got the following output "bobaaa" and "baaa."
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is online

  • member icon


Reputation: 3991
  • View blog
  • Posts: 12,317
  • Joined: 25-December 09

Re: Comparison of Original and Reversed String; Palindrome Checker

Posted 02 September 2013 - 09:39 AM

Did you fix the following issue?
   for(i=LenPhrase-1;i>=1;i--)
      cout<<phrase.word[i];
   {  
      ReversedPhrase=ReversedPhrase+phrase.word[i];
   }


Notice the difference in the indent level. This is why properly formatting your code is essential it should help spot problems like this.

Jim
Was This Post Helpful? 0
  • +
  • -

#6 sundet  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-September 13

Re: Comparison of Original and Reversed String; Palindrome Checker

Posted 02 September 2013 - 11:08 AM

View Postjimblumberg, on 02 September 2013 - 09:39 AM, said:

Did you fix the following issue?
   for(i=LenPhrase-1;i>=1;i--)
      cout<<phrase.word[i];
   {  
      ReversedPhrase=ReversedPhrase+phrase.word[i];
   }


Notice the difference in the indent level. This is why properly formatting your code is essential it should help spot problems like this.

Jim

I hadn't fixed it. I fixed it and made a change to the for loop. Things are working well now. That was a good lesson on how proper spacing can make errors easier to find.

Is there any situation where it makes sense to break up a for loop as was done in my original code? It is my guess there is no purpose for doing that and it would have been nice if Visual Studio would have prohibited me from compiling or given me a warning message.
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is online

  • member icon


Reputation: 3991
  • View blog
  • Posts: 12,317
  • Joined: 25-December 09

Re: Comparison of Original and Reversed String; Palindrome Checker

Posted 02 September 2013 - 11:51 AM

Quote

Is there any situation where it makes sense to break up a for loop as was done in my original code?

An if() statement, like most other control statements in C/C++, don't actually require braces when only one line is to be included in the body of the statement.

Quote

It is my guess there is no purpose for doing that and it would have been nice if Visual Studio would have prohibited me from compiling or given me a warning message.

Your original code had no errors, it was valid code, so there is no way any compiler could warn you of the problem. And yes there are times when there is a purpose for the kind of code you posted. Adding braces alters the scope and can be used to encapsulate sections of your code.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1