Question is :
Get two strings from user. Join first string with the second string with a space. Store in another string and display it.
Example:
Enter first string: Hello
Enter second string: World
Hello World
(built in function for string concatenation is not allowed)


im newbie in c++ and arrays please tell me whats the problem in my code :(

#include<iostream.h>
#include<conio.h>
#include<stdio.h>

void main()

{
clrscr();

char a[100];
char b[100];
char c[200];

int s;

cin.getline(a,100);

cin.getline(b,100);


for (s=1;c[s]!=NULL;s++)
{
c[s]=a[s]+b[s];
}


puts(c);



getch();
}

Edited 5 Years Ago by AmerJamil: n/a

I am not sure why you needed the clrscr(); so i removed it, but otherwise here is the modified code with some comments, any questions, feel free to ask :)

#include<iostream>
#include<conio.h>
#include<stdio.h>

void main()

{

char a[100];
char b[100];
char c[200];

int s, i; // i is defined for the entire block

std::cin.getline(a,100);

std::cin.getline(b,100);


for (s = 0, i = 0; s < strlen(a); s++, i++) // strlen() determines length of string
{
c[i]=a[s]; // set c = a for each character
}

for (s = 0;c[i]!=NULL; s++, i++)
{
c[i]=b[s]; // continue where first for loop left off and store c += b
}



puts(c);



getch();
}
Comments
We also format our code properly
We don't do people's homework for them.

one thing whats strlen condition is doing in loop? and in problem statement it is stated that built in function for string concatenation is not allowed.. so im not sure to add this function is allowed or not :(..so is there is any alternate method without using strlen function?.. plus there should be a space between both strings..

for example
first string user entered: Hello
2nd string: World

new string should be: Hello World

i.e space between two strings not like this HelloWorld

Edited 5 Years Ago by AmerJamil: n/a

Since you like to be repetitive:

Do you know how to copy from one array to another? You should, after your other thread.

All you need to do is take that solution and modify/expand it to fit this situation.

Comments
:D

in the following code, it is storing a chracters not the b chracters into c.. please tell me where im wrong :

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>

void main()

{clrscr();

char a[100];
char b[100];
char c[200];

int s, i;

int count=1;
int countb=0;

cin.getline(a,100);

cin.getline(b,100);



for (i=0;a[i]!='\0';i++)
{count++;}

countb=count;

for (i=0;b[i]!='\0';i++)
{countb++;}


cout<<count<<endl;

cout<<countb<<endl;


for (i=0;i<count;i++)
{c[i]=a[i];}


int r=0;

for (i=count;i<countb;i++)
{
c[i]=b[r++];
}

puts(c);

getch();
}
cout<<c[6]<<c[7];

it is displaying 2nd array..not by puts(c) :(

i.e by cout of c in loop... why not it is showing through puts?

Edited 5 Years Ago by AmerJamil: n/a

This question has already been answered. Start a new discussion instead.