0

So, I am trying to write an strncpy function. I want user to input the number of characters to be copied from source. I am doing something wrong, but I can't understand what. This is what I tried to do:

#include <stdio.h>
#include <string.h>
#define ARR_SIZE 20

int main() {
    char string[ARR_SIZE];
    int n, m;
    char s1[4], s2[4], nstr[m];
    printf("Enter the string:");
    gets(string);
    printf("The length of the string is: %ld\n", strlen(string));

    strcpy(s1, s2);
    printf("The original string is: %s\n", string);
    printf("The copy of the original string is: %s\n", string);
    
    printf("How many characters do you want to take from this string to create another string? Enter: \n");
    scanf("%d", &n);
    strncpy(nstr, s1, m);
    printf("%s\n", nstr);
}

(On top I tried some strlen and strcpy functions.) EDIT: I totally forgot to write what was the problem. Problem is I can't get the new string which is named nstr in my code. Even though I printed it out.

  • 1
    What's the problem? – comonadd Apr 02 '21 at 14:17
  • 2
    Try to tackle problems *one at a time.* You are trying to implement user input and string-copying at the same time and something is going wrong. Write two programs, one that takes user input, and another that manipulates hard-coded strings. Only when both are working perfectly should you try to dovetail them. – Beta Apr 02 '21 at 14:28

1 Answers1

1

first of all, the whole code is just a bad practice.

Anyway, here is my take on your code which copies n characters of an input string to string_copy

#include <stdio.h>
#include <string.h>
#define ARR_SIZE 20

int main() {
    char string[ARR_SIZE];
    int n;
    printf("Enter the string:");
    gets(string);
    printf("The length of the string is: %ld\n", strlen(string));

    printf("The original string is: %s\n", string);

    printf("How many characters do you want to take from this string to 
    create another string? Enter: \n");
    scanf("%d", &n);
    if(n > strlen(string)){
        n = strlen(string);
        printf("you are allowed to copy maximum of string length %d\n", n);
    }

    char string_copy[n];
    strncpy(string_copy, string, n);
    printf("%s\n", string_copy);
}

note that using deprecated functions such as gets() isn't safe. use scanf() or fgets() instead.

refer to why you shouldn't use gets()

LazerDance
  • 177
  • 1
  • 8
  • `"%ld"` does not certainly match the type returned by `strlen()`. Who or what text suggested that specifier? – chux - Reinstate Monica Apr 02 '21 at 18:02
  • this is just a dummy example and doesn't guarantee 100% correctness. it just answers the question with the least modifications possible – LazerDance Apr 02 '21 at 20:16
  • Rephrase: `char string_copy[n]; strncpy(string_copy, string, n); printf("%s\n", string_copy);` is bad code (UB) regardless of what `n` is. `"%s"` expects a _string_ and `string_copy` is not a string. – chux - Reinstate Monica Apr 02 '21 at 20:28
  • I don't know what's the point of this discussion. if we wanna make perfect code then I would add a lllot more conditions and constrians, such as (n isn't allowed to be less than 0). Just get over it. otherwise a simple answer code could triple in size. We are not writing a library code here ;) – LazerDance Apr 03 '21 at 09:43