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 6379145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:05:59+00:00 2026-05-25T02:05:59+00:00

I have a question regarding C++. This is my current function: string clarifyWord(string str)

  • 0

I have a question regarding C++. This is my current function:

string clarifyWord(string str) {
    //Remove all spaces before string
    unsigned long i = 0;
    int currentASCII = 0;

    while (i < str.length()) {
        currentASCII = int(str[i]);
        if (currentASCII == 32) {
            str.erase(i);
            i++;
            continue;
        } else {
            break;
        }
    }

    //Remove all spaces after string
    i = str.length();
    while (i > -1) {
        currentASCII = int(str[i]);
        if (currentASCII == 32) {
            str.erase(i);
            i--;
            continue;
        } else {
            break;
        }
    }
    return str;
}

Just to get the basic and obvious things out of the way, I have #include <string> and using namespace std; so I do have access to the string functions.

The thing is though that the loop is quitting and sometimes skipping the second loop. I am passing in the str to be " Cheese " and it should remove all the spaces before the string and after the string.

In the main function, I am also assigning a variable to clarifyWord(str) where str is above. It doesn’t seem to print that out either using cout << str;.

Is there something I am missing with printing out strings or looping with strings? Also ASCII code 32 is Space.

  • 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-25T02:06:00+00:00Added an answer on May 25, 2026 at 2:06 am

    Okay so the erase function you are calling looks like this:

    string& erase ( size_t pos = 0, size_t n = npos );
    

    The n parameter is the number of items to delete. The npos means, delete everything up until the end of the string, so set the second parameter to 1.

    str.erase(i,1)
    

    [EDIT]

    You could change the first loop to this:

    while (str.length() > 0 && str[0] == ' ')
    {
       str.erase(0,1);
    }
    

    and the second loop to this:

    while (str.length() > 0 && str[str.length() - 1] == ' ')
    {
       str.erase(str.length() - 1, 1);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Again I have a question regarding this plugin: http://t.wits.sg/2008/06/20/jquery-progress-bar-11/ What I want to achieve
I have this very straight forward question regarding Thread and Timer classes in Java
I have question regarding the use of function parameters. In the past I have
I have seen this question posed regarding Silverlight 2 but I have not seen
I have question regarding the CreateWindowEx function. I have 2 windows, a main one
I have a question regarding vectors: If I have a std::vector<MyClass> will this vector
I have a question regarding handling errors in a J2EE application. Our current application
I have seen different questions regarding this, but I still find this topic to
I hope this is allowed but I have a number of questions regarding Facebook
I was reading this question (which you do not have to read because I

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.