Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1023865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:41:28+00:00 2026-05-16T11:41:28+00:00

I have a simple function, that checks if the strings given match a certain

  • 0

I have a simple function, that checks if the strings given match a certain condition, then generate a 3rd string based on the 2 ones received as arguments. The 3rd string is fine but when I return it it suddenly turn into “\n“.

string sReturn = "";
if (sText.size() != sPassword.size()) {
     //Checks to see if the texts match a condition
     return sReturn;
}
for (int foo = 0; foo < sText.size(); foo++) {
    sReturn = "";
    sReturn += (char)sText[foo] ^ (char)sPassword[foo];
}
return sReturn;

In the for sReturn is fine and has the right contents, but as soon as it exists the loop, the debugger suddenly tells me it’s contents are “\n“. What am I doing wrong ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T11:41:29+00:00Added an answer on May 16, 2026 at 11:41 am
    1. You don’t have to initialize string
      with empty character array like:

      std::string sReturn = "";
      

      Default constructor meant to do it
      for you and is much more efficient.
      Correct code:

      std::string sReturn;
      
    2. Assigning an empty string to sReturn
      on each iteration in your loop is
      incorrect. Not to mention that to
      clear a string you have to call
      std::string::clear ():

      sReturn = "";
      

      Correct code:

      sReturn.clear (); 
      

      But this should be removed from the
      loop at all in your case.

    3. There is not need to explicitly
      convert the result of operator []
      (size_t) into a character because it
      is a character:

      sReturn += (char)sText[foo] ^ (char)sPassword[foo];
      

      Correct code:

      sReturn += sText[foo] ^ sPassword[foo];
      
    4. Using post-increment in for
      loop is not necessary. It makes an
      extra copy of the “foo” on every
      increment:

      for (int foo = 0; foo < sText.size(); foo++)
      

      This probably will be optimized by
      compiler but you have to get rid of
      this bad habit. Use
      pre-increment instead. Correct
      code:

      for (int foo = 0; foo < sText.size(); ++foo)
      
    5. Calling std::string::size () on
      every iteration when string size
      does not change is not efficient:

      for (size_t foo = 0; foo < sText.size(); ++foo)
      

      Better code:

      for (size_t foo = 0, end_foo = sText.size(); foo < end_foo; ++foo)
      

      Note size_t type. You cannot store
      string size in 32-bit signed integer
      as it has not enough capacity to
      store large number. Correct type is
      size_t, which is returned by
      std::string::size () method.

    Taking into account all above, the correct function should look something like this:

    std::string
    getMixedString (const std::string & text, const std::string & password)
    {
        std::string result;
        if (text.length () != password.length ())
            return result;
        for (size_t pos = 0, npos = text.length (); pos < npos; ++pos)
            result += text[pos] ^ password[pos];
        return result;
    }
    

    But there is a problem if you want the final string to be human-readable. Using eXclusive OR (XOR) operator on two ASCII characters might or might not give you human readable character or even an ASCII character. So you may end up having resulting string that contains newlines, unreadable characters, some garbage whatsoever.

    To solve this, you have to come up with some better algorithm to generate a string basing on two other strings. For example, you may use MD5 hash of both strings or encode them in base64.

    Good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this very simple br2nl function that I use to take a string
I have a simple function in my library that checks the validity of an
I have a simple regex which checks an entire string for a function declaration.
I have this simple code that speaks for itself. <script language='javascript"> function check() {}
I have a simple function that looks at an incoming mySQL data type and
I have a simple function that works in chrome, but in Internet explorer (9)
I have a simple function that returns an NSString after decoding it. I use
I have a simple function that shows loading spinner while fetching data (usually takes
I have a simple function that I'd like to test (perhaps mostly to appease
I have a very simple function that takes a list of comma separated (x,y)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.