now...like...i need to concatenate the two words that the user enters, s1 and s2 and output them...i thought i needed to use strcat but like...i dont really understand how to do that here, do i need to make a new c string variable or...? ah i hate this stuff lol but i need to use c strings

i also somehow need to test to see if the word is a palindrome. same backwards and forwards, like mom, wow, racecar

can i get pointed in the right direction here? i dont understand c++ that well even with extra tutoring every week (networking major :p)

so can i get some help please?

#include "stdafx.h"
#include<iostream> 
#include<cstring> 

using namespace std; 

int main( ) 
{ 
     char s1[100], s2[100;
     int length1, length2; 
     int count = 0; 


  // Initialize the two strings 
     strcpy(s1,"Black"); 
     strcpy(s2,"Black"); 

  while(count < 2) 
  { 
      if(count > 0) 
      // After the first time ask the user to enter them 
      { 
          cout << "Now I let you enter two strings \n"; 
          cout << "Enter the first string, then the second \n"; 
          // Another way to initialize the two strings 
          cin >> s1 >> s2; 
      } 
      // Find their lengths 
      length1 = strlen(s1); 
      length2 = strlen(s2); 
      // Compare their length 
      if(length1 == length2) 
     { 
              cout << "The two strings are the same length, are they the same? \n"; 
      } 
      // See if they are the same 
      if(! strcmp(s1, s2) ) 
      { 
           cout << "The two strings: "; 
           cout << s1 << " and " << s2 << " are the same \n"; 
      } 
      else 
      { 
          cout << "The two strings: "; 
          cout << s1 << " and " << s2 << " are NOT the same \n"; 
      } 
      count++; 
   } 


  system ("PAUSE");
  return 0; 
}

>>i need to concatenate the two words
Yes, use strcat() for that. strcat(s1, s2); That works as long as the length of the two strings do not exceed the space allocated for s1 (which is 100).

>>also somehow need to test to see if the word is a palindrome
create another buffer, such as s3, copy the string backwards, then compare it with the original string. Another way is to use two pointers, one that starts at the beginning of the string and the second to start at the end of the string. Then compare the two pointers one character at a time.

Although dragon is right about palindromes; you do not need to go as far as creating a third string; you can use a while or for loop to compare the the strings simulataneously and return false the moment a pair of characters differ.

i.e.

int x, y;
string s1,s2;
bool palindrome = true;
cin>>s1>>s2;

if(s1.length() ==s2.length()) {
    y = s2.length()-1;
    for(x = 0; x < (s1.length()/2-1); x++) {
           if(s1[x] != s2[y]) {
                palindrome = false;
                x = s1.length();
           }
    else
           y--;
     }

if(palindrome)
cout<<"palindrome!";

else
cout<<"not a palindrome";

@seethelite
i appreciate the code, of course, but i dont really understand it, i get a few compile errors that i dont know what to do about

Error 1 error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)

and

Error 3 fatal error C1075: end of file found before the left brace '{'

trying to look at your code and follow it in my head so i can get this stuff but its just not happening lol

inputting that into my code, which i'll include at the bottom, would i need to use new names for like s1 and s2? like are those different variables?


@ancientdragon how exactly does one copy a string backwards and compare it -_-

i want to go back to arrays ;_; i understood them

#include "stdafx.h"
#include<iostream> 
#include<cstring> 

using namespace std; 

int main( ) 
{ 
     char s1[100], s2[100], s3[100];
     int length1, length2; 
     int count = 0; 


  // Initialize the two strings 
     strcpy(s1,"Black"); 
     strcpy(s2,"Black"); 

  while(count < 2) 
  { 
      if(count > 0) 
      // After the first time ask the user to enter them 
      { 
          cout << "Now I let you enter two strings \n"; 
          cout << "Enter the first string, then the second \n"; 
          // Another way to initialize the two strings 
          cin >> s1 >> s2; 
      } 
      // Find their lengths 
      length1 = strlen(s1); 
      length2 = strlen(s2); 
      // Compare their length 
      if(length1 == length2) 
     { 
              cout << "The two strings are the same length, are they the same? \n"; 
      } 
      // See if they are the same 
      if(! strcmp(s1, s2) ) 
      { 
           cout << "The two strings: "; 
           cout << s1 << " and " << s2 << " are the same \n"; 
      } 
      else 
      { 
          cout << "The two strings: "; 
          cout << s1 << " and " << s2 << " are NOT the same \n"; 
      } 
      count++; 
   } 

	strcat(s1,s2);

	cout << s1 << endl;

  system ("PAUSE");
  return 0; 
}

Here is how I would write a paladrone function

bool IsPalindrome(const char* string)
{
    int len = strlen(string)/2;
    const char*p1 = string;
    const char* p2 = string + strlen(string)-1;
    while( len-- > 0)
    { 
        if( *p1++ != *p2--)
            return false;
    }
    return true;
}

Heh, I got confused when you said two inputs, because a palindrome is only one O____O

anyways, this will compile on devc++

#include <iostream>
using namespace std;

main() {
int x, y;                           //these are your counters
string s1;                       //string input
bool palindrome = true; //flag for palindrome
cin>>s1;


    y = s1.length()-1;       //intializes y at the end of string s1
    for(x = 0; x < (s1.length()/2-1); x++) {  //starts a count from the beginning to half way through s1
           if(s1[x] != s1[y]) {                          //because s1[x] and s2[y] are the same distance from their respective ends, if they are not equal, then s1 is not a palindrome
                palindrome = false;
                x = s1.length();                          //exits loop
           }
    else
           y--;                                                 //if the two equal, subtract from y to keep up with x
     }

if(palindrome==true)
cout<<"palindrome!";

else
cout<<"not a palindrome";

       system("PAUSE");
       }

>>anyways, this will compile on devc++
You forgot to include <string> :)

line 4: must be int main() because c++ does not support default return types.

It might compile, but does it work? If I enter "wom" will it produce a false positive? The loop on line 12 will not execute because (3/2)-1 = 0.

Heh, I'm probally gonna get hell for this, but the class string is included in <iostream>, using namespace std;(I'm not quite sure which mind you). As for int main(), I'll take your word for(and pretty much all of my observations) because Devc++ does infact allow "default return types," and yes, it does produce a false positive >_<.

I should probally say that I can only partially solve some of these problems as my teacher never really goes into "how" things work, that is to say, my knowledge of the inner workings of c++ are mediocore to say the least. BUT, I do know to solve problems quite well, and I can say with certainty that my palindrome function that uses stacks works perfectly with your case :).

>>but the class string is included in <iostream>
If that is true with Dev-C++ then that is some compiler-specific thing which no other compiler in the world has implemented.

>>BUT, I do know to solve problems quite well, and I can say with certainty that my palindrome function that uses stacks works perfectly with your case

Then you should have posted it :)

Guess I got the wuss's compiler then, lol.

oh and that would require .h file I made, which would be of little use to atreides because he does not have it >_>(okay so I could have given it to him but thats to much of a hassle)

Guess I got the wuss's compiler then, lol.

oh and that would require .h file I made, which would be of little use to atreides because he does not have it >_>(okay so I could have given it to him but thats to much of a hassle)

Dev-C++ is getting a little ancient now because it isn't being updated. Download free Microsoft VC++ 2008 Express for more current implementation of c++ language. Or even Code::Blocks IDE uses more current version of minig compiler (I think that's it).

The problem being I do not (nor will I ever probally) have admin rights on this computer because my dad forgot the password(GAH), I tried using eclipse C++ IDE but I couldn't figure out how to link it to minGW or cygwin(the guides were for an older version), I also tried to get emacs(failed miserably) so I can't help but have stockholm's syndrome right now with new compilers :/. BUT, there is hope, as I will be getting a laptop in the coming months!

This article has been dead for over six months. Start a new discussion instead.