0

my goal is to return the desired size of sentence. i.e input size = 9,full sentence = "stack over flow", will return "stack ov" (one for 0/), what i got so far

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int size;
    char* A;

    printf("insert size you desired +1 :\n");
    scanf ( "%d%*c", &size);
    A = calloc(size,sizeof(char));

    printf("insert full size sentence :\n");
    fgets ( A, size, stdin);

    printf("desired size sentence : %s\n",A);
    return 0;
}

solved

Hary
  • 21
  • 6
  • Storing full sentence in a reduced buffer, allocated to the requested size, will overflow. – Frankie_C Mar 27 '16 at 16:02
  • 1
    To read a full line use `fgets()` instead of `scan()`ing in a "string". Note that `fgets()` reads the final new-line character as well. – alk Mar 27 '16 at 16:04
  • `%s` reads until a white-space, so if you enter more non white-space `char`s then the buffer's size -1, the buffer gets overflowed and the code provokes undefined behaviour by this. – alk Mar 27 '16 at 16:08
  • 1
    You probably need `if (fgets(A, size, stdin) != 0) { printf(…); printf(…); }`. Using `%s` with `scanf()` without specifying how many bytes are OK is tantamount to suicide; if the user says `9` and then types `Heap-Over-Flow!` then the 15 characters plus null will be stored in the 9 bytes allocated — which is not a recipe for happiness. Sadly, you can't tell `scanf()` dynamically how big the string is; you have to format a string with the correct size information and use that. But it is far too easy to wreck your program as written. The `fgets()` code has minor issues but is 'safe'. – Jonathan Leffler Mar 27 '16 at 16:11
  • @user3121023 i misstype that %s, i tried gets(A) but somehow it skip the insert sentence part – Hary Mar 27 '16 at 16:15
  • @user3121023 now it works, but it still giving 2 output ? it gives. "stack ov stack ov" – Hary Mar 27 '16 at 16:21
  • 2
    Do not use `gets()` it's error prone, and as well not part of C any more. – alk Mar 27 '16 at 16:26
  • `getline(3)` is probably a safer alternative when it comes to reading from stdin; see http://man7.org/linux/man-pages/man3/getline.3.html – asamarin Mar 27 '16 at 16:30
  • @user3121023 my bad, im writing output 2 times, ofc it will. – Hary Mar 27 '16 at 16:32
  • See http://stackoverflow.com/a/2239571/856199 on how to print a specific number of character. Note that in your code, you should not use `size` as the `calloc()` length, because when the input string is longer than `size` characters, you write more chars into it than it can hold. Use some other large number instead in `calloc()` and `fgets()`. Use `size` only in `prinf()` to limit the amount of printed characters. – Nikos C. Mar 27 '16 at 16:42
  • 2
    Possible duplicate of [Is there a way to specify how many characters of a string to print out using printf()?](http://stackoverflow.com/questions/2239519/is-there-a-way-to-specify-how-many-characters-of-a-string-to-print-out-using-pri) – Nikos C. Mar 27 '16 at 16:42

0 Answers0