19

I have not been able to find any information with a web-search. Where should I be looking?

Jean-Bernard Pellerin
  • 12,556
  • 10
  • 57
  • 79
user2341069
  • 389
  • 5
  • 8
  • 14

7 Answers7

26
char myString[256]; // Input string
char dest[256];     // Destination string

strncpy(dest, myString, 10);
dest[10] = 0; // null terminate destination
Robb1
  • 4,587
  • 6
  • 31
  • 60
Lefteris E
  • 2,806
  • 1
  • 24
  • 23
  • quick question, could I do strncpy(myString, myString,10) ? – user2341069 May 02 '13 at 22:14
  • 3
    if you want to truncate the string to 10 chars then put myString[10] = 0; (This given that myString points to memory and not a string lettral) – Lefteris E May 02 '13 at 22:14
  • @user2341069 no you cant, as per the man page "The strings may not overlap, and the destination string dest must be large enough to receive the copy." As myString is a pointer to a char array in memory, using the same pointer twice means that they "overlap" in memory, and thus will cause an error. Essentially, this means that the src can not be a substring of dst or vice versa. – TonyArra May 02 '13 at 22:18
  • 1
    Is the null terminate character not '\0' rather than just a 0? – TafT Feb 12 '16 at 11:24
  • 2
    @TafT they are equivalent – Lefteris E Apr 29 '16 at 08:28
  • @LefterisE Thank you. It seems they are equivalent but '\0' is often used to indicate a character instead of a number. This question http://stackoverflow.com/questions/16955936/string-termination-char-c-0-vs-char-c-0 has a few sources in it. – TafT May 03 '16 at 14:45
  • I'm afraid I disagree: `strncpy()` has widely misunderstood semantics and is **very** error prone. **DO NOT USE THIS FUNCTION**. Learn why here: https://randomascii.wordpress.com/2013/04/03/stop-using-strncpy-already/ – chqrlie Dec 04 '16 at 13:00
7
char source[] = "abcdefthijklmn";
char target[100];

strncpy(target, source, 10);
target[10] = '\0'; // IMPORTANT!
tianz
  • 2,155
  • 2
  • 19
  • 28
4

Adding to the above answers:

char* someString = "your string goes here";

int main() 
{
  int n = 10;
  printf("(%.*s)\n", n, someString);

  return 0;
}
StackUseR
  • 884
  • 1
  • 11
  • 40
3

There are many different ways to achieve your goal:

  • You can use snprintf (safest):

    char source[] = "abcdefthijklmn";
    char target[100];
    
    snprintf(target, sizeof target, "%.10s", source);
    
  • You can use strncat if you know the destination has at least 11 elements:

    char source[] = "abcdefthijklmn";
    char target[100];
    
    *target = '\0';
    strncat(target, source, 10);
    
  • You can use strlen and memcpy (same assumption about the size of destination):

    char source[] = "abcdefthijklmn";
    char target[100];
    size_t len = strlen(source);
    
    if (len > 10)
        len = 10;
    memcpy(target, source, len);
    target[len] = '\0';
    
  • You can use a loop (same assumption about the size of destination):

    char source[] = "abcdefthijklmn";
    char target[100];
    size_t i;
    for (i = 0; i < 10; i++) {
        target[i] = source[i];
    }
    target[i] = '\0';
    
  • You could but should not use strncpy()

chqrlie
  • 131,814
  • 10
  • 121
  • 189
0

If you're looking for a good source, here's an example of an online man page you can use: http://linux.die.net/man/3/strncpy

Important to note: you could also use memcpy instead of strncpy, but it requires you to add your own null-terminating byte.

Warning: If there is no null byte among the first n bytes of src, the string placed in dest will not be null-terminated.

Hence, memcpy and strncpy work almost the same here, and memcpy is more efficient and less prone to error.

TonyArra
  • 10,607
  • 1
  • 30
  • 46
0

I got it to work like this.

   # include <stdio.h>
   # include <string.h>
   //Strings. String lenght terminator.
   //KHO2016.no1. mingw (TDM-GCC-32) . c-ansi .
   int main ()
   {
   //declare
   char src_str[20],dst_str[10];

   //valuate
   printf ("Enter a sentance of 20 letters\n");
   gets (src_str);
   strcpy (dst_str,src_str);

   //calculate
   dst_str [10] ='\0'; // from the "www.stack overflow"
   printf ("%s",dst_str);
   printf ("\n");

   //terminate
   return 0;
   }
0

You can also use sprintf with .10 precision format:

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

int main(void)
{   
    char source[] = "abcdefghijklmnopqrstuvwxyz";
    char dest[11];
    memset(dest, '\0', sizeof(dest));

    sprintf(dest, "%.10s", source);

    printf("%s", dest); // abcdefghij
    return 0;
}
w.b
  • 11,026
  • 5
  • 30
  • 49