So, I have seen this strcpy implementation in C:
void strcpy1(char dest[], const char source[])
{
int i = 0;
while (1)
{
dest[i] = source[i];
if (dest[i] == '\0')
{
break;
}
i++;
}
}
Which to me, it even copies the \0 from source to destination.
And I have also seen this version:
// Move the assignment into the test
void strcpy2(char dest[], const char source[])
{
int i = 0;
while ((dest[i] = source[i]) != '\0')
{
i++;
}
}
Which to me, it will break when trying to assign \0 from source to dest.
What would be the correct option, copying \0 or not?
The code should look like as follows:
char * strcpy(char *strDest, const char *strSrc)
{
assert(strDest!=NULL && strSrc!=NULL);
char *temp = strDest;
while(*strDest++ = *strSrc++); // or while((*strDest++=*strSrc++) != '\0');
return temp;
}
You can NOT delete the second line char *temp = strDest; and directly return strDest. This will cause error for the returned content. For example, it will not return correct value (should be 22) will checking the length of returned char *.
char src_str[] = "C programming language";
char dst_str[100];
printf("dst_str: %d\n", strlen(strcpy(dst_str, src_str)));
Both copy the terminator, thus both are correct.
Note that strcpy2() does the assignment (the copying) first, then the comparison. So it will copy the terminator before realizing it did, and stopping.
Also, note that functions whose names start with str are reserved, so neither of these are actually valid as "user-level" code.
You're wrong. Both copy the \0 (NUL terminator) character. You have to copy the NUL terminator character always or your string will be broken: you'll never know when/where it ends.
Both copy the terminator, thus both are correct.
strcpy2() does the copying first, then the compares. Thus it will copy the terminator and stops.
The functions whose names start with str are reserved, so use any other variables or naming types
It is recommended not to advance the input pointers to the source and destination memory spaces, since the pointers will be used in main right away.
I've mentioned alternate methodical syntax, where in case someone might wonder the code output.
void strcpy1(char * s, char * p)
{
char * temp1 = s;
char * temp2 = p;
while(*temp1 != '\0')
{
*temp2 = *temp1;
temp1++;
temp2++;
}
*temp2 = '\0';
}
void main()
{
char * a = "Hello";
char b[10];
strcpy1(a,b);
printf("%s", b);
return 0;
}
Both strcpy1() and strcpy2() does the same. Both copy the NUL character to the end of the destination array.
Here is full implementation. You do not have to consider the \0 at the end in the first string, it will be copied automatically from the second string as per logic
//str copy function self made
char *strcpynew(char *d, char *s){
char *saved = d;
while ((*d++ = *s++) != '\0');
return saved; //returning starting address of s1
}
//default function that is run by C everytime
int main(){
//FOR STRCPY
char s1[] = "rahul"; //initializing strings
char s2[] = "arora"; //initializing strings
strcpynew(s1, s2);
printf("strcpy: %s\n", s1); //updated string after strcpy
}
You can use this code, the simpler the better !
Inside while() we copy char by char and moving pointer to the next. When the last char \0 will pass and copy while receive 0 and stop.
void StrCopy( char* _dst, const char* _src )
{
while((*_dst++ = *_src++));
}
char * strcpy(char *strDest, const char *strSrc)
{
assert(strDest!=NULL && strSrc!=NULL);
assert(strSrc + strlen(strSrc) < d || strSrc > strDest); // see note
char *temp = strDest;
while(*strDest++ = *strSrc++)
;
return temp;
}
// without the check on line 4, the new string overwrites the old including the null deliminator, causing the copy unable to stop.
Both copy the '\0'. That's what you have to do if you want to fully emulate the original strcpy
Related
I have written following c code to update the char array rb but it is printing garbage value
#include <stdio.h>
void update(char* buff){
char word[] = "HII_O";
buff = word;
return;
}
int main(){
char rb[6];
update(rb);
rb[5] = '\0';
printf("[%s]\n",rb);
return 0;
}
The restriction is we can't use any other library. So how to solve this
Within the function update the parameter buff is a local variable of the function that will not be alive after exiting the function.
You can imagine the function call the following way
update( rb );
//...
void update( /*char* buff*/){
char *buff = rb;
char word[] = "HII_O";
buff = word;
return;
}
As you see the original array was not changed.
That is at first the pointer buff was initialized by the address of the first element of the source array rb.
char *buff = rb;
and then this pointer was reassigned with the address of the first element of the local character array word
buff = word;
What you need is to copy characters of the string literal "HII_O" into the source array rb using standard string function strcpy or strncpy.
For example
#include <string.h>
#include <stdio.h>
void update(char* buff){
strcpy( buff, "HII_O" );
}
int main(){
char rb[6];
update(rb);
printf("[%s]\n",rb);
return 0;
}
As you cannot use any library function, just do de copying cell by cell, change
void update( /*char* buff*/){
char *buff = rb;
char word[] = "HII_O";
buff = word;
return;
}
(you cannot assign arrays as a whole in C)
into:
void update(char *buff) {
char *word = "HII_O";
int index;
/* copy characters, one by one, until character is '\0' */
for (index = 0; word[index] != '\0'; index = index + 1) {
buff[index] = word[index];
}
/* index ended pointing to the next character, so we can
* do the next assignment. */
buff[index] = '\0'; /* ...and copy also the '\0' */
}
buff is a local variable to the function. It is initialized to point to the first element of the rb array in main but changes to buff will not change the rb array. So
buff = word;
makes buff point to the string literal "HII_O" but there is no change to the rb array.
The normal solution would be
void update(char* buff){
strcpy(buff, "HII_O");
}
However, you write ...
The restriction is we can't use any other library.
Well, in order to set a fixed value like your code does, you don't need any library function.
You don't any need other variables, string literals, etc.
Just simple character assignments like:
void update(char* buff){
buff[0] = 'H';
buff[1] = 'I';
buff[2] = 'I';
buff[3] = '_';
buff[4] = 'O';
buff[5] = '\0';
}
int main(){
char rb[6];
update(rb);
printf("[%s]\n",rb);
return 0;
}
I want to compare a String in another String like "ther" in "motherfathersister" and the result would be either null ( no match ) or the remaining characters after the last occurence ( in this example) "thersister".
I have changed the implemantation of strstr from string.h but the result is everytime the same.
Where i have made a mistake?
Thanks
char *stRstr(char *s, char *m)
{
char *last=NULL;
size_t n = strlen(m);
while(*s)
{
if(!memcmp(s++,m,n))
{
last=s-1;
}
}
return last;
}
Thanks Guys
I found my mistake...
i didnt't called my function "stRstr" but "strstr"... And this function is already implemented...
Thanks for your help, without your feedback, in terms of that my functions returns the right result, i would have been lost...
As mentioned in the comments, your code will access s beyond its bounds (consider what memcmp does when s points to the last non-'\0'-character). So I'd get rid of the memcmp-usage and I'd scan the string from backwards.
In analogy to strstr, which quickly scans the string looking for a single-character match and compares then the rest, see the following code that does exactly the same in a backwards manner.
Hope it helps.
const char *stRstr(const char *s, const char *m)
{
const char* ptr = s + strlen(s);
size_t mlen = strlen(m);
while (ptr-- != s) { // until ptr reaches the beginning of s
if (*ptr==*m) { // single character match (first characters of s and m)?
if (strncmp(ptr,m,mlen)==0) // check the remainder
return ptr;
}
}
return NULL;
}
void printLast(const char* s) {
if (s)
printf("last: %s\n", s);
else
printf("string not found.\n");
}
int main() {
const char* s = "this is the first, this is the last";
const char* m = "this";
const char* last = stRstr(s, m);
printLast(last);
last = stRstr(s, "not contained");
printLast(last);
}
Your function should work (the problem is elsewhere) but it has some issues:
using memcmp() is incorrect as it may access s beyond its last element. you should use strncmp() instead or add a test on the lengths.
the posted code would not find the last occurrence of the empty substring and return NULL, where it should return a pointer to the trailing null byte.
for consistency, you should call it strrstr() or my_strrstr().
Here is a faster and safer version:
char *my_strrstr(const char *s, const char *m) {
char *last = NULL;
size_t n = strlen(m);
while ((s = strchr(s, *m)) != NULL) {
if (!strncmp(s, m, n))
last = (char *)s;
if (*s++ == '\0')
break;
}
return last;
}
How would I append a char to string that has no initial value in the following method I've started:
void append(char a) {
const char *str;
char *ret = malloc (strlen(str)+2);
strncpy(str,strlen(str),ret);
ret[strlen(str)-2] = a;
ret[strlen(str)-1] = 0;
printf("%s", str);
}
I've tried a few different answers to other questions but none have worked, please help.
Since the pointer str is not initialized, you can't add characters to what it points at. What makes sense depends on what you're going to do next. Since you don't return a value from the function, you can't access the string to which a is appended unless append calls some other function.
char *str; is not a string. It is a mnemonic that says that *str will give you a value which is supposedly a character.
str is a pointer that points at random. Make it point to an allocated bunch of memory first.
Do something like- str = malloc(100); and then do a *(str+1) = a and then a *(str+2) = '\0' to NULL terminate the string.
how about something like:
char * append(char a, const char * str)
{
char *ret = malloc (strlen(str)+2);
strncpy(str,strlen(str),ret);
ret[strlen(str)-2] = a;
ret[strlen(str)-1] = 0;
return ret;
}
I have writen a code to split the string with multiple char delimiter.
It is working fine for first time of calling to this function
but i calling it second time it retuns the correct word with some unwanted symbol.
I think this problem occurs because of not clearing the buffer.I have tried a lot but cant solve this. please help me to solve this problem.
char **split(char *phrase, char *delimiter) {
int i = 0;
char **arraylist= malloc(10 *sizeof(char *));
char *loc1=NULL;
char *loc=NULL;
loc1 = phrase;
while (loc1 != NULL) {
loc = strstr(loc1, delimiter);
if (loc == NULL) {
arraylist[i]=malloc(sizeof(loc1));
arraylist[i]=loc1;
break;
}
char *buf = malloc(sizeof(char) * 256); // memory for 256 char
int length = strlen(delimiter);
strncpy(buf, loc1, loc-loc1);
arraylist[i]=malloc(sizeof(buf));
arraylist[i]=buf;
i++;
loc = loc+length;
loc1 = loc;
}
return arraylist;
}
called this function first time
char **splitdetails = split("100000000<delimit>0<delimit>hellooo" , "<delimit>");
It gives
splitdetails[0]=100000000
splitdetails[1]=0
splitdetails[2]=hellooo
but i called this second time
char **splitdetails = split("20000000<delimit>10<delimit>testing" , "<delimit>");
splitdetails[0]=20000000��������������������������
splitdetails[1]=10����
splitdetails[2]=testing
Update:-
thanks to #fatelerror. i have change my code as
char** split(char *phrase, char *delimiter) {
int i = 0;
char **arraylist = malloc(10 *sizeof(char *));
char *loc1=NULL;
char *loc=NULL;
loc1 = phrase;
while (loc1 != NULL) {
loc = strstr(loc1, delimiter);
if (loc == NULL) {
arraylist[i]=malloc(strlen(loc1) + 1);
strcpy(arraylist[i], loc1);
break;
}
char *buf = malloc(sizeof(char) * 256); // memory for 256 char
int length = strlen(delimiter);
strncpy(buf, loc1, loc-loc1);
buf[loc - loc1] = '\0';
arraylist[i]=malloc(strlen(buf));
strcpy(arraylist[i], buf);
i++;
loc = loc+length;
loc1 = loc;
}
}
In the caller function, i used it as
char *id
char **splitdetails = split("20000000<delimit>10<delimit>testing" , "<delimit>");
id = splitdetails[0];
//some works done with id
//free the split details with this code.
for(int i=0;i<3;i++) {
free(domaindetails[i]);
}free(domaindetails);
domaindetails=NULL;
then i called the same for the second as,
char **splitdetails1= split("10000000<delimit>1000<delimit>testing1" , "<delimit>");
it makes error and i can't free the function.
thanks in advance.
Your problem boils down to three basic things:
sizeof is not strlen()
Assignment doesn't copy strings in C.
strncpy() doesn't always nul-terminate strings.
So, when you say something like:
arraylist[i]=malloc(sizeof(loc1));
arraylist[i]=loc1;
thisdoes not copy the string. The first one allocates the size of loc1, which is a char *. In other words, you allocated the size of a pointer. You want to allocate storage to store the string, i.e. using strlen():
arraylist[i]=malloc(strlen(loc1) + 1);
Note the + 1 as well, because you also need room for the nul-terminator. Then, to copy the string you want to use strcpy():
strcpy(arraylist[i], loc1);
The way you had it was just assigning a pointer to your old string (and in the process leaing the memory you had just allocated). It's also common to use strdup() which combines both of these steps, i.e.
arraylist[i] = strdup(loc1);
This is convenient but strdup() is not part of the official C library. You need to assess the portability needs of your code before you consider using it.
Additionally, with strncpy(), you should be aware that it does not always nul-terminate:
strncpy(buf, loc1, loc-loc1);
This copies less bytes than were in the original string and doesn't terminate buf. Thus, it's necessary to include a nul terminator yourself:
buf[loc - loc1] = '\0';
This is the root cause of what you are seeing with the garbage. Since you didn't nul terminate, C doesn't know where your string ends and so it keeps on reading whatever happens to be in memory.
Language: C
I am trying to program a C function which uses the header char *strrev2(const char *string) as part of interview preparation, the closest (working) solution is below, however I would like an implementation which does not include malloc... Is this possible? As it returns a character meaning if I use malloc, a free would have to be used within another function.
char *strrev2(const char *string){
int l=strlen(string);
char *r=malloc(l+1);
for(int j=0;j<l;j++){
r[j] = string[l-j-1];
}
r[l] = '\0';
return r;
}
[EDIT] I have already written implementations using a buffer and without the char. Thanks tho!
No - you need a malloc.
Other options are:
Modify the string in-place, but since you have a const char * and you aren't allowed to change the function signature, this is not possible here.
Add a parameter so that the user provides a buffer into which the result is written, but again this is not possible without changing the signature (or using globals, which is a really bad idea).
You may do it this way and let the caller responsible for freeing the memory. Or you can allow the caller to pass in an allocated char buffer, thus the allocation and the free are all done by caller:
void strrev2(const char *string, char* output)
{
// place the reversed string onto 'output' here
}
For caller:
char buffer[100];
char *input = "Hello World";
strrev2(input, buffer);
// the reversed string now in buffer
You could use a static char[1024]; (1024 is an example size), store all strings used in this buffer and return the memory address which contains each string. The following code snippet may contain bugs but will probably give you the idea.
#include <stdio.h>
#include <string.h>
char* strrev2(const char* str)
{
static char buffer[1024];
static int last_access; //Points to leftmost available byte;
//Check if buffer has enough place to store the new string
if( strlen(str) <= (1024 - last_access) )
{
char* return_address = &(buffer[last_access]);
int i;
//FixMe - Make me faster
for( i = 0; i < strlen(str) ; ++i )
{
buffer[last_access++] = str[strlen(str) - 1 - i];
}
buffer[last_access] = 0;
++last_access;
return return_address;
}else
{
return 0;
}
}
int main()
{
char* test1 = "This is a test String";
char* test2 = "George!";
puts(strrev2(test1));
puts(strrev2(test2));
return 0 ;
}
reverse string in place
char *reverse (char *str)
{
register char c, *begin, *end;
begin = end = str;
while (*end != '\0') end ++;
while (begin < --end)
{
c = *begin;
*begin++ = *end;
*end = c;
}
return str;
}