user14377467
user14377467

Reputation: 31

How to copy the string that remains after using strncpy

I am learning C and want to learn how I can copy the remaining characters leftover in the string after using strncpy. I would like to have the string Hello World broken down into two separate lines.

For example:

int main() {
    char someString[13] = "Hello World!\n";
    char temp[13];

    //copy only the first 4 chars into string temp
    strncpy(temp, someString, 4);

    printf("%s\n", temp);          //output: Hell
}

How do I copy the remaining characters (o World!\n) in a new line to print out?

Upvotes: 1

Views: 1010

Answers (4)

DipStax
DipStax

Reputation: 502

What you can do is to push your pointer of n character and copy the size - n caractere:

size_t n = 4; // nunmber caractere to copy first 
size_t size = 13; // string length

char someString[size] = "Hello World!\n";
char temp[size];
char last[size - n]; // the string that contain the reste

strncpy(temp, someString, n); // your copy
strncpy(last, someString + n, 13 - n); // copy of reste of the string

Upvotes: 0

chqrlie
chqrlie

Reputation: 144695

The one thing you should learn about strncpy is never use this function.

The semantics of strncpy are counter-intuitive and poorly understood by most programmers. It is confusing and error prone. In most cases, it does not do the job.

In your case, it copies the first 4 bytes and leaves the rest of temp uninitialized. You might have known this, but still invoked undefined behavior by passing temp to printf as a string argument.

If you want to manipulate memory, use memcpy and memmove and be careful about null terminators.

As a matter of fact, the string "Hello world!\n" has 13 characters and a null terminator, requiring 14 bytes in memory. Defining char someString[13] = "Hello World!\n"; is legal but it makes someString not a C string.

#include <stdio.h>
#include <string.h>

int main() {
    char someString[14] = "Hello World!\n";
    char temp[14];

    memcpy(temp, someString, 4); //copy only the first 4 chars into string temp
    temp[4] = '\0';              // set the null terminator
    printf("%s\n", temp);        //output: Hell\n

    strcpy(temp + 4, someString + 4);  // copy the rest of the string
    printf("%s\n", temp);        //output: Hello World!\n\n

    memcpy(temp, someString, 14); //copy all 14 bytes into array temp
    printf("%s\n", temp);        //output: Hello World!\n\n

    // Note that you can limit the number of characters to output for a `%s` argument:
    printf("%.4s\n", temp);      //output: Hell\n
    return 0;
}

You can read more about strncpy here:

Upvotes: 2

alex01011
alex01011

Reputation: 1702

First of all, char someString[13] , you don't have enough space for the string Hello World\n, since you have 13 characters but you need at least size of 14, one extra byte for the NULL byte, '\0'. You better off let the compiler decide the size of the array, wouldn't be prone to UB that way.

To answer your question, you can just use printf() to display the remaining part of the string, you only need to specify a pointer to the element you want to start at.

In addition, strncpy() doesn't NULL terminate tmp, you are gonna have to do that manually if you want functions like printf() or puts() to function properly.

#include <stdio.h>
#include <string.h>

int main(void)
{
    char someString[] = "Hello World!\n";
    char temp[14];

    strncpy(temp,someString,4);

    temp[4] = '\0'; /* NULL terminate the array */

    printf("%s\n",temp);
    printf("%s",&someString[4]); /* starting at the 4th element*/

    return 0;
}

Upvotes: 1

Jose Manuel de Frutos
Jose Manuel de Frutos

Reputation: 994

In your case you could try something like:

char   temp2[13];
strncpy(temp2, &someString[4], 9);

By the way you are missing a semicolon:

char   someString[13] = "Hello World!\n";

Upvotes: 0

Related Questions