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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:47:33+00:00 2026-06-09T17:47:33+00:00

I know a string concatenation question has been asked to death in SE. But

  • 0

I know a string concatenation question has been asked to death in SE. But to the best of my knowledge,I’ve gone through all the questions that could help me, in vain.

This is what I am hoping to accomplish with this program:

Initially I have a=0 and b=1, for n=0 and n=1 respectively.

For the next input i.e from n=3 onwards, my result should be concatenation of the previous two strings. (Like a Fibonacci sequence; only the addition is replaced by concatenation)

So,for example:
For n=3, my output should be “10”.
For n=4, my output should be “101”
For n=5, my output should be “10110”

There is no logical problem with the code I’ve written,but I’m getting a SIGSEGV error and I don’t see why.

#include <iostream>
#include<new>
#include<string.h>

using namespace std;

int main()
{
    long int n,i;
    char *a="0";
    char *b="1";
    char *c=new char[100000];

cout<<"Enter  a number n:";
cin>>n;

for(i=0;i<n;i++)
{

    strcat(b,a);
    strcpy(a,b);

}
cout<<"\nRequired string="<<b;

}

What am I doing wrong?

  • 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-09T17:47:34+00:00Added an answer on June 9, 2026 at 5:47 pm

    strcat(b,a); invokes undefined behaviour because b points to a string literal.

    char * strcat ( char * destination, const char * source );
    

    Concatenate strings
    Appends a copy of the source string to the destination string.

    Since this is C++, I suggest you use std::string and the + operator. Or a std::stringstream.

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

Sidebar

Related Questions

I know this type of question has been asked before, but I could not
from this question , I know that a const string can be the concatenation
i know that .NET String class uses such behaviour, but I can't find any
I know how to save string from EditTexts on android but my problem is:
I know there are similar questions to this one, but I haven't found any
Is there a JPA concat operator for string concatenation? I know there is a
We know that using string concatenation to form SQL queries renders a program vulnerable
I know you should use a StringBuilder when concatenating strings but I was just
We know that string is a reference type , so we have string s=God
Does anybody know why String.compareTo is not programmed to behave more graciously in regards

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.