[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Copy of a string

Posted on 2011-03-05
5
Medium Priority
?
279 Views
Last Modified: 2012-05-11
Please tell me how I can make a copy of a given string, at run time.

If my function takes char *str as a parameter, then I want make a local copy of str within the function.

Thanks!
0
Comment
Question by:dshrenik
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 16

Expert Comment

by:Peter Kwan
ID: 35043582
0
 

Author Comment

by:dshrenik
ID: 35043593
I don't want to use library functions.

Will it work if I find the length of the string, then dynamically allocate a char array and assign its value to the original?

If possible, please provide some sample code.

Thanks!
0
 
LVL 16

Accepted Solution

by:
Peter Kwan earned 668 total points
ID: 35043670
You can do it this way (pseudo-code only):
char *new_str;
int i = 0;
new_str = (char*) malloc(sizeof(char));
new_str[i] = orig_str[i];
while (orig_str[i] != NULL) {
     new_str = (char*)realloc(new_str, (i+2)*sizeof(char));
     new_str[i] = orig_str[i];
     i++;
} 
new_str[i]=0;

Open in new window

0
 
LVL 11

Assisted Solution

by:Deepu Abraham
Deepu Abraham earned 668 total points
ID: 35044187
another way if implementing this . here is the snippet
char* strcpy_new(char* dest,const char* source)
{
	if ((dest == NULL)|| (source == NULL))
		return NULL;
        //if you know the length then you could check it here..
	int i = 0;
	while(source[i]!='\0')
	{
		dest[i] = source[i];
		i++;
	}
	if(dest[i] != '\0')
		dest[i] = '\0';

	return dest;
}

Open in new window

0
 
LVL 1

Assisted Solution

by:hogwell
hogwell earned 664 total points
ID: 35047085
// A simpler, more efficient version that creates a copy of the input string on the heap.
char* copy_a_string(const char *str)
{
    const char *str1=str;
    size_t len;
    char *copy_of_str;
    char *str2;
    while (*str1) ++str1;
    len = str1 - str;
    copy_of_str = (char*) malloc((len+1)*sizeof(char)); // Or use "copy_of_str = new char[len+1];" in C++
    for (str1=str,str2=copy_of_str; *str1; )
      *str2++ = *str1++;
    *str2 = '\0';
    return copy_of_string;
}
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question