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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:11:28+00:00 2026-06-15T18:11:28+00:00

I need to read string of characters (with spaces) and reverse it. First i

  • 0

I need to read string of characters (with spaces) and reverse it.

First i read a string of characters from console.

char array[100];

for(int i = 0; i < sizeof(array); i++)
    array[i] = '0';

for(int i = 0; i < sizeof(array); i++)
    cout<<array[i]<<" ";

cout<<"\n\nenter your string: ";

cin.getline(array, 100);

cout<<"\n";

for(int i = 0; i < sizeof(array); i++)
    cout<<array[i]<<" ";   

Then i create and fill indexes array. (to remember space spots)

cout<<"\n\nindexes: ";

int indexes[20];
for(int i = 0; i < 20; i++)
{
    indexes[i] = 0;
}

// filling the indexes array
int j = 0;
for(int i = 0; i < sizeof(array); i++)
{
    if(array[i] == '0') break;
    if(array[i] == ' ')  indexes[j++] = i;
}   
// validating indexes array
for(int i = 0; i < sizeof(indexes); i++)
{
    //if(indexes[i] == 0) break;
    cout<<indexes[i]<<" ";
}  

Here is the begining of my (i guess not smart) algorythm. I want to reverse first word.
Seems like this chunk never executes. Why?

// first word
int j1 = indexes[0]-1;
for(int i = 0; i > indexes[0]; i++)     
{
    new_array[i] = array[j1-i];
            cout<<"\naction\n";
}

Print new_array to console:

for(int x = 0; x < sizeof(new_array); x++)
{
    if(new_array[x] == '0') break;
    cout<<new_array[x]<<" ";
}  

The result is:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

enter your string: this is my string

t h i s i s m y s t r i n g 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

indexes: 4 7 10 17 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -858993460 -858993460 100 -85
8993460 -858993460 100 -858993460 -858993460 100 -858993460 -858993460 193628786
0 544434464 1931508077 1852404340 805314663 808464432 808464432 808464432 808464
432 808464432 808464432 808464432 808464432 808464432 808464432 808464432 808464
432 808464432 808464432 808464432 808464432 808464432 808464432 808464432 808464
432 -858993460 -742139265 6421640 18244041 1 9932784 9928256 -742139185 0 0 2129
674240 -1920 570026249 0 6422528 0 6421580 0 6421712 18223374 -759898201 0 64216
48 18244541

new array:

Why the indexes array contains these numbers after zeros? It’s length is only 20.
And why the new_array is not printed?

I know that my solution is very very cumbersome)

  • 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-15T18:11:29+00:00Added an answer on June 15, 2026 at 6:11 pm

    Your intuition is right: the loop isn’t being executed. The problem is in the line:

    for(int i = 0; i > indexes[0]; i++)
    

    The comparison should be i < indexes[0].

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

Sidebar

Related Questions

I need to read 16 bits from the binary file as std::string or char
I need to read in an expression from a file using a string stream
I need to read/write a string of characters using the IBM-1047 character encoding. But
I am writing a function in which I need to read a string contains
I need to read in a string and then an integer until the user
I need to read webpage and store its content in string for further processing.
Imagine the following: you read in a string with scanf() but you only need
I need a function to return a string that will only be accessed read-only.
I need to read certain statistics from iw_statistics structure, here's the code: struct net_device
I need to read data from a text file where the field lengths and

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.