473,408 Members | 1,747 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

copy string

39
Hi I have this simple program which copies one string to another i.e newstring---->old . When I print out old string it is succesfully copied. But when I use a program to test if those two strings are equal, It gives me false value. Can someone guide me. I think it has something to do with how I allocate memory.

Here is my program


#include<iostream>
#include<string>
using namespace std;
int main()
{
char *newstring="This is the string I want to copy into old";
char *old;
char test='a';
int index=0;
bool result;

// Copying newstring to the old string

old = new char[100] ;
while(test != '\0')
{



old[index]=newstring[index];
test=old[index];
index=index+1;


}

// Testing if the old string is equal to the newstring

test='a';

while(test != '\0')
{
test=newstring[index];

if(newstring[index] == old[index])
result = true;
else
{
result=false;
break;
}
index =index+1;
}

//Checking the result of comparision

cout<<"\n The result is "<<result<<endl;

}
Apr 29 '07 #1
3 3130
Savage
1,764 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. old = new char[100] ;
  2. while(test != '\0')
  3. {
  4.    old[index]=newstring[index];
  5.    test=old[index];
  6.    index=index+1;
  7. }
There is a simple function declared in string.h called strcat which appends one string to the end of another.So if we set old[0] to '\0' and then call the function we will sucsessfully copy one string to another.If u wish to test it there is also a nice function called strcmp which tests strings and return 0 if they match.

Savage
Apr 29 '07 #2
JosAH
11,448 Expert 8TB
Expand|Select|Wrap|Line Numbers
  1. old = new char[100] ;
  2. while(test != '\0')
  3. {
  4.    old[index]=newstring[index];
  5.    test=old[index];
  6.    index=index+1;
  7. }
There is a simple function declared in string.h called strcat which appends one string to the end of another.So if we set old[0] to '\0' and then call the function we will sucsessfully copy one string to another.If u wish to test it there is also a nice function called strcmp which tests strings and return 0 if they match.

Savage
There's another simple function for copying a string: strcpy(). You don't have to
manually set the first char to '\0' then. The reason of failing to compare those
two strings succesfully is the fact that variable 'index' isn't reset to 0 (zero)
before the comparing while loop starts.

kind regards,

Jos
Apr 29 '07 #3
atlkhan
39
Thanks man , I really appreciate it
Apr 29 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

22
by: Shea Martin | last post by:
I have a String class (I know I am re-inventing the wheel, yes I have heard of boost, and of QString). My copy constructor does a deep (strcpy) of the char *_buffer member. I have a member...
42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
5
by: LRS Kumar | last post by:
Stoustrup, in The C++ Programming Language, has the following example in 11.7: string g (string arg) // string passed by value using copy constructor { return arg; //string...
16
by: bluekite2000 | last post by:
I want Matrix A(B) to create shallow copy of B but A=B to create deep copy of B. Is that bad design? Why and why not?
8
by: Jesper | last post by:
Hi, Does the concept "copy constructor" from c++ excist in c#. What is the syntax. best regards Jesper.
14
by: Arne | last post by:
In C++ we have a copy constructor. What is the equivalent in .Net? Would that be a clone method?
12
by: CMirandaman | last post by:
Sounds like a stupid question I know. I can tell that they are used to copy strings. But what is the difference between x = y; versus x = String.Copy(y); Or are they essentially the same?
18
by: sd2004 | last post by:
could someone please show/help me to copy all element from "class dog" to "class new_dog" ? Note: "class new_dog" has new element "age" which should have value "my_age"...
18
by: active | last post by:
Console.WriteLine(String.Equals(s, s.Clone.ToString)) s is type string. This returns True. If Clone really made a new copy I'd expect False.
11
by: Dijkstra | last post by:
Hi folks! First, this is the code I'm using to expose the problem: ------------------------------------------------------------------ #include <functional> #include <string> #include...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.