1

Create a function in C that takes a string as a parameter and copy it to a new string. If the original string is "abc", then the new string should be "aabbcc", if the original string is "4", then the newstring should be 44 etc. I believe i understand the concepts needed to solve a problem like this, but i just can't get the new string to be printed in the console. Here is my function:

void eco(char * str)
{

    int count = 0; 

    /*Counts the number of symbols in the string*/
    while(*(str + count) != '\0')
    {
       count++;              
    }

    /*Memory for the new string, wich should be 6 chars long ("aabbcc").*/
    char * newstr = malloc(sizeof(char *) * (count * 2)); 

    /*Creating the content for newstr.*/
    while(count > 0)
    {
       *newstr = *str;  //newstr[0] = 'a'
       *newstr++;       //next newstr pos
       *newstr = *str;  //newstr[1] = 'a'
       *str++;          //next strpos
       count--;         
    }

    /*I can't understand why this would not print aabbcc*/
    printf("%s", newstr);

    /*free newstr from memory*/
    free(newstr);
}

I have tried to print every char individually inside the while loop that creates the content for newstr, and that work. But when i try with the "%s"-flag i either get strange non-keyboard symbols or nothing at all.

5
  • Please fix the code ident! Commented Jun 7, 2014 at 20:08
  • Don't forget to allocate space for, and initialise, the 0 to terminate the string. Commented Jun 7, 2014 at 20:08
  • malloc(sizeof(char *) is almost certainly not what you want. Commented Jun 7, 2014 at 20:10
  • Also, you're modifying the value of newstr, so that it's no longer pointing to the beginning of the string when you want to display it. Commented Jun 7, 2014 at 20:11
  • I have tried with adding one additional char in malloc for the '\0' and also i've tried to "rewind" newstr to the beginning by using newstr--; in a loop before the print. Same problem still though. Commented Jun 7, 2014 at 20:14

3 Answers 3

2

I can't understand why this would not print "aabbcc"

It wouldn't do it for two reasons:

  • You are not passing a pointer to the beginning of the string, and
  • Because you did not add a null terminator

To fix the first problem, store the pointer to the block allocated to newstr in a temporary before doing the increments.

To fix the second problem, add *newstr = '\0' after the loop, and adjust malloc call to add an extra char for the terminator.

// Do not multiply by sizeof(char), because the standard requires it to be 1
// You used sizeof(char*), which is wrong too.
char * newstr = malloc((count * 2) + 1);
char *res = newstr; // Store the original pointer
// Your implementation of the actual algorithm looks right
while (...) {
    ... // Do the loop
}

*newstr = '\0';
printf("%s\n", res); // Pass the original pointer
Sign up to request clarification or add additional context in comments.

1 Comment

Two more things, he forgot a second newstr++, and there's no need to dereference *newstr++ (at the OP code sample).
1

Your loop advances newstr, so after it completes, it's not pointing at the beginning of the string anymore. You need to save the original pointer to use for printing.

Comments

0

For a start this line

char * newstr = malloc(sizeof(char *) * (count * 2)); 

should be

char * newstr = malloc(1 + (count * 2));

to include the null character

Then you forgot to add it

Also newstr points to the end to the new string

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.