c++


Copying a string char by char in another string


I have a string type and I am trying to copy this into another string type char by char but when I try to display the output of new copied it is coming as blank. Where as when I try to output char by char for that copied string length its alright. Please see this small code and output for better understanding.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name = "Tom";
string temp;
int i = 0;
for(; i < name.length(); ++i)
{
temp[i] = name[i];
}
cout<<name<<endl; //gives the output Tom
cout<<temp<<endl; //gives blank
for(int i = 0; i < name.length(); ++i)
{
cout<<temp[i]; //gives output char by char
}
}
Output :
Tom
Tom
I know there are other possible ways to achieve what I am trying to do but just out of thought tried this and it didn't work. So looking for some explanation.
Note that temp is empty, its length is 0. Then temp[i] = name[i]; leads to undifined behavior; anything is possible but nothing is guaranteed.
You can give it an initial length like:
string name = "Tom";
string temp(name.length(), '\0');
You have to allocate data for the temp string to change any data in it.
string temp(name.length(),'\0');

Related Links

Amazon AWS CPP SDK Get S3 Bucket Download Progress
IDEA CLion 2016.2.1 can't recognize the function exit()
Audacity PCM Data [on hold]
Best practice to set up qt application that works for different DPI
openssl fips error on arm Linux
C++ - Pretty way to make frequencies add up to 1
integerset obtaining the given array
Arduino Invalid conversion from 'char' to 'char*'
virtual ClistCtrl with checkboxes on displayed report list style
Error when modifying vector member of another class
Interrupt Service Routine can't be called directly
Lambda capture by default reference vs named
convert bitset<64> to string every 8 bit
argument of type “int” incompatible with parameter of type “int”
When is the correct time to call restoreState for QHeaderView?
How does it pass lvalue reference to std::forward with CRTP?

Categories

HOME
react-native
cron
rabbitmq
screensaver
jupyter-notebook
owasp
mongoose
web-crawler
integration-testing
binary
visual-studio-mac
graph-algorithm
cordova-plugins
pstricks
graphviz
xhtml
servo
bpm
access-control
appcelerator-titanium
cllocationmanager
filenet-p8
accurev
recurly
greedy
xmlstarlet
cookie-session
fxml
servicestack.redis
modeling
mpd
direct3d9
digestive-functors
grayscale
fancytree
modelio
proof-of-correctness
smartmeter
fuzzy
angularjs-service
android-7.1-nougat
c#-to-vb.net
volume-rendering
rpath
deferred-loading
powergrep
grape
grid-layout
rodbc
ionic-view
oracle-streams
stl-format
listboxitem
angular-component-router
spsite
pdf-extraction
johnny-five
traffic-simulation
approximation
soundex
kismet-wireless
cleartool
coderunner
kiosk-mode
jquery-dynatree
ews-managed-api
httplib
string-search
kindle-fire
data-sharing
impactjs
mathgl
commission-junction
rs485
kendo-tooltip
jquery-textext
icarousel
converters
ejbql
mbprogresshud
google-instant-previews
rakefile

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile