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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:13:39+00:00 2026-06-02T03:13:39+00:00

Please help with debugging. It gives me an error ‘string subscript out of range

  • 0

Please help with debugging. It gives me an error ‘string subscript out of range error’.

The program needs to sort the text using insertion sort algorithm.

Here is the code:

#include<iostream>
#include<string>
using namespace std;

void insertionSort(string &text, int size) {
  char temp;
  int i;
  for(int j=1;j<size;j++)
  {
    //text.push_back(temp);
    temp=text[j];
    i=j-1;
   while(i>=0 && text[i]>temp)
 {

 text[i+1]=text[i];
  i--;
 }
    text[i+1]=temp;
  }
}

int main()
{
  string text="this a just text need to be sorted";

  int size = text.length();
  insertionSort(text,size);
  cout<<text<<endl;
  return 0;
}

debug assertion Failed!

Line:1441:

Expression: string subscript out of range

I supposed to change text[i+1]=text[j] to text[i+1]=text[i];

  • 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-02T03:13:40+00:00Added an answer on June 2, 2026 at 3:13 am

    Replace

    while(text[i]>temp && i>=0)
    

    with

    while(i>=0 && text[i]>temp)
    

    Reason:

    When i becomes negative, ie i == -1, then first check for i>=0 instead of doing check for text[i]>temp (which tries to access array element at position -1 and gives out of range).

    EDIT:

    also replace

    text[i+1]=text[j];
    

    with

    text[i+1]=text[i];
    

    Why so ? : In insertion sort if we have entries greater than text[j] in the lower part (ie. 0 to j-1), then we need to push these entries ahead and stop at point when we no longer have elements bigger than text[j].

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

Sidebar

Related Questions

Please help me with using the DrScheme built-in function filter. create a function hello
Please help me to fix the CSS error in profile on my site (IE).
Please help I am getting the following error when I am binding the drop
Please help me out with an algorithm for the following problem - Given a
Please help me with the following implementation of Counting Sort in Java. I am
I'm getting this error, not really sure why. Please help, this is urgent. UPDATE
Please dont mark this as duplicate I am getting this error while debugging my
I wonder if anyone could please help me I have been using HTML tidy
Please help to the newbie in WPF! I need to build a TreeView with
Please help! I need to produce a Crystal Report with multiple data columns, but

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.