Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C++
#include <iostream.h>
#include <conio.h>
#include <string.h>
void main()
{
clrscr();
char first[20];
char seond[20];
cout<<"Enter string ";
cin>>first;
strcpy(seond,first);
cout<<seond;
getch();
}

The above code is from copying string from first to second
i want to display the output as

ali ahmed

but stcpy function ignores the characters after space.
Posted
v3
Comments
Sergey Alexandrovich Kryukov 13-Dec-12 1:25am    
Please fix code formatting. You include file don't show because you forgot to escape angular brackets...
And add "pre" tags.
--SA
Sergey Alexandrovich Kryukov 13-Dec-12 1:26am    
By the way, there is no a question. No, strcpy does not ignore characters after space...
--SA

1 solution

cin does not read characters after space. Use cin.getline() instead of cin if you want to read more than one word. Also sync the input stream.

XML
#include <iostream.h>
#include <conio.h>
#include <string.h>
void main()
{
clrscr();
char first[20];
char seond[20];
cin.sync();
cout<<"Enter string";

cin.getline(first,20);

strcpy(seond,first);

cout<<seond;
getch();
}
 
Share this answer
 
v2
Comments
Maximilien 13-Dec-12 6:18am    
When coding in C++ there are no reason (*) to use char arrays; use std::string instead.
(*) YES, I know there are situations where you need to use char arrays and char*, but in this example, and when learning C++ one must use the proper ways.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900