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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:12:21+00:00 2026-06-12T20:12:21+00:00

Examples: char test1[] = ; char test2[] = hello z; char test3[] = hello

  • 0

Examples:

char test1[] = "               ";
char test2[] = "   hello  z";
char test3[] = "hello world   ";
char test4[] = "x y z ";

Results:

"               "
"   olleh  z"
"olleh dlrow   "
"x y z "

The problem:

Reverse every world in a string, ignore the spaces.

The following is my code. The basic idea is to scan the string, when
finding a word, then reverse it. The complexity of the algorithm is
O(n), where n is the length of the string.

How do verify it? Is there a better solution?

void reverse_word(char* s, char* e)
{
    while (s < e)
    {
        char tmp = *s;
        *s = *e;
        *e = tmp;
        ++s;
        --e;
    }
}

char* word_start_index(char* p)
{
    while ((*p != '\0') && (*p == ' '))
    {
        ++p;
    }

    if (*p == '\0')
        return NULL;
    else
        return p;
}

char* word_end_index(char* p)
{
    while ((*p != '\0') && (*p != ' '))
    {
        ++p;
    }

    return p-1;
}

void reverse_string(char* s)
{
    char* s_w = NULL;
    char* e_w = NULL;
    char* runner = s;

    while (*runner != '\0')
    {
        char* cur_word_s = word_start_index(runner);
        if (cur_word_s == NULL)
            break;
        char* cur_word_e = word_end_index(cur_word_s);
        reverse_word(cur_word_s, cur_word_e);
        runner = cur_word_e+1;
    }
}
  • 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-06-12T20:12:22+00:00Added an answer on June 12, 2026 at 8:12 pm

    Your code seems correct, but it’s plain C. In C++, using the same approach, it could look something like this:

    #include <iostream>
    #include <string>
    #include <iterator>
    #include <algorithm>
    #include <cctype>
    
    int main()
    {
        std::string str = "    cat cow dog wolf     lobster";
        std::string result;
        auto it = str.begin();
    
        while (it != str.end()) {
    
            while (it != str.end() && isspace(*it)) {
                result += *it;
                ++it;
            }
    
            auto begin = it;
            while (it != str.end() && !isspace(*it)) {
                ++it;
            }
            auto end = it;
    
            result.append(std::reverse_iterator<decltype(end)>(end),
                          std::reverse_iterator<decltype(begin)>(begin));
    
            // if you want to modify original string instead, just do this:
            std::reverse(begin, end);
        }
    
        std::cout << result <<'\n';
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to check equality of two String objects independent of the char order? Examples:
Following is the example I have reproduced: charName :: Char -> String charName 'a'
I'm just trying to compile the hello world example of boost.python WITHOUT using all
I have a string, the string contains for example Hello\nThis is a test.\n. I
I am trying to use this command: sed -i 's#\{test1\}#test2#' /example/myfile.txt To replace instances
I read a lot of examples to retrieve a java string in C/C++ code,
How can I replace a char in a string with a string? For Example:
char test[]={abcde}; char* test1={xyz}; memcpy(test+5,test1,3); printf(%s,test); I'm trying to grasp how exactly memcpy works
I am trying to create a simple hello world example using swapcontext() Here is
Take the following C code as an example: char buffer1[5]; int* ret; printf(Buffer1 is:

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.