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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:52:17+00:00 2026-05-28T07:52:17+00:00

I am writing a simple program, where all the ‘spaces’ will be replaced by

  • 0

I am writing a simple program, where all the ‘spaces’ will be replaced by ‘%20’.

#include <iostream>
#include <string>

using namespace std;

int main (int argc, char* argv[]){

    string input;
    cout << "please enter the string where spaces will be replaced by '%20'" << endl;
    getline(cin, input);
    //count the number of spaces
    int countSpaces = 0;
    for (int i = 0 ; i < input.length() ; i++){
        if (input[i] == ' '){
            countSpaces++;

        }

    }

    int size = input.length() + (2 * countSpaces) + 1;
    //char cstr1[size];
    char *cstr1 = new char[size];
    char *cstr = cstr1;
    for (int i = 0 ; i < input.length() ; i++){
        if(input[i] == ' '){ 
            *cstr++ = '%';
            *cstr++ = '2';
            *cstr++ = '0';
        }               
        else{   
            *cstr++ = input[i];

        }

    }
    *cstr == '\0';

    cout << cstr1 << endl;
    delete[] cstr1;

   return 0;

}

I get the following strange behavior:

  1. With the test input "this is strange " I get "this%20is%20strange%20%20his is" , where I just expect "this%20is%20strange%20%20"

  2. If I hard code the same string, I get the correct results.

  3. Replacing char *cstr1 = new char[size]; with char cstr1[size]; & removing the delete[] while still fetching the input via getline also removes the error.

I am using i686-apple-darwin10-g++-4.2.1:

Any help is much appreciated.

  • 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-28T07:52:18+00:00Added an answer on May 28, 2026 at 7:52 am

    The last line must be *cstr = ‘\0’; not ==

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

Sidebar

Related Questions

I'm writing a simple program that will run entirely client-side. (Desktop programming? do people
i was writing a program and using double.Try Parse to check if a string
I'm writing a simple C program using fork() to create a binary tree of
I am writing a program for a class that is very simple. All it
I am writing a simple program to flip all the bits in a file,
I'm writing simple program to communicate between smart devices and I receive 11001 when
I'm writing a simple program to browse the local network and pass on filenames
I'm writing a simple program to help generate orders for a game I'm a
I'm writing a simple program. There is only one class in it. There is
I am writing a simple program, which is trying to find next palindrome number

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.