-5

How can I back up the string in its original form? I'm using strncpy() but, however when I try to print the sentence the original text is altered. Here is an example: if I entered "This is a sample text" for gets and ask to print the sentence, the console prints "TTTTs is a ample ttxt". Can someone tell how to make it so that the sentenceBackup variable has a backup and the sentence is correctly displayed as entered.

    //String variable to contain the user input.
    char sentence[] = "";
    char sentenceBackup[] ="";

    //this variable tracks the size of the user input.
    int sentenceLength;

    //ask the user forsinput
    printf("Enter a free formed sentence that needs to be sorted: \n");

    //accept the user entry into sentence.
    //scanf is deprecated since C11.
    gets(sentence);

    // keep a backup for further operation.
    strncpy(sentenceBackup, sentence, findLength(sentence));


    //display the sentence entered.
    printf("The sentence is : %s\n", sentence);

Ps: If I take off the strncpy() method, the source text,i.e sentence variable is correctly displayed.

Rak
  • 9
  • 3
  • 2
    When you declare an array in C without specifying the array size, C assumes that it was intended to be the same size as its initializer. In your case, 1 `char` long. Try giving your array declarations a large size, like `[2048]`; otherwise the second character written to `sentenceBackup` likely overwrites the first character of `sentence`. Think of the arrays as being adjacent in memory and only 1 char long, each...where does the second char written by your `strncpy` go? – lockcmpxchg8b Aug 30 '18 at 03:19
  • 1
    `char sentence[] = ""` is too small to take input from `gets(sentence);` – chux - Reinstate Monica Aug 30 '18 at 03:34
  • Why the comment `scanf is deprecated in C11`? It is not (see §7.21.6.4 of [n1570](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf)). Of course `gets` is absent from C11 (which has `fgets` in §7.21.7.2) so you should [not](https://en.cppreference.com/w/c/io/gets) use it. Please **edit your question** to improve it! – Basile Starynkevitch Aug 30 '18 at 04:52
  • BTW, your question has no [MCVE]. It should has a *complete* program. That is another reason to improve your question – Basile Starynkevitch Aug 30 '18 at 05:06

2 Answers2

2
  1. The size of sentence is insufficient to hold the input.

  2. Second, gets is deprecated, so do not use it. From this online C reference:

    The gets() function does not perform bounds checking, therefore this function is extremely vulnerable to buffer-overflow attacks. It cannot be used safely (unless the program runs in an environment which restricts what can appear on stdin). For this reason, the function has been deprecated in the third corrigendum to the C99 standard and removed altogether in the C11 standard. fgets() and gets_s() are the recommended replacements. Never use gets().

Community
  • 1
  • 1
P.W
  • 26,289
  • 6
  • 39
  • 76
  • Also the C11 standard [n1570](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) does not even *mention* `gets` – Basile Starynkevitch Aug 30 '18 at 04:55
  • 1
    @BasileStarynkevitch: [N1570: footnote 404 (Annex K.3.5.4.1](https://port70.net/~nsz/c/c11/n1570.html#note404): _The `gets_s` function, unlike the historical `gets` function, …_ Also, at the other end of the document, [Foreword §6](https://port70.net/~nsz/c/c11/n1570.html#Forewordp6): _removed the `gets` function (``)_ But I grant you, these are not exactly major references, and certainly not definitions of what the archaic `gets()` function does. – Jonathan Leffler Aug 30 '18 at 05:04
  • And the index of n1570 don't mention `gets` – Basile Starynkevitch Aug 30 '18 at 05:05
0

If using getline or strdup is acceptable for you, then use them!

If you are not allowed to use them, you should at least heap-allocate the strings (with calloc, but don't forget the extra byte for the ending NUL byte) and don't forget to free them later. Of course you need to "backup" explicitly (by appropriately copying the original string).

If heap allocation is not allowed, be sure to declare large enough buffers. I would suggest at least 128 bytes.

Please look into some C reference site (study the documentation of every function that you use) and later refer to the C11 standard n1570. You'll see that gets is no more part of C and is forbidden since dangerous. Use fgets at least (and preferably getline).

Compile your code with all warnings and debug info, e.g. gcc -Wall -Wextra -g with GCC. Improve it to get no warnings at all. The use the debugger (gdb or something else) and read How To Debug Small Programs.

Be aware that UTF-8 is used everywhere in 2018. So strlen don't give the number of characters (since a character like Ê or needs several bytes in UTF-8), only the byte length.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547