Click here to Skip to main content
12,508,630 members (54,054 online)
Rate this:
 
Please Sign up or sign in to vote.
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 12-Dec-12 19:14pm
ALIWAZ407
v3
Comments
   
Please fix code formatting. You include file don't show because you forgot to escape angular brackets...
And add "pre" tags.
--SA
   
By the way, there is no a question. No, strcpy does not ignore characters after space...
--SA

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.

#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();
}
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160927.1 | Last Updated 13 Dec 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100