0

I have the following task at the university 1. terms:

  1. Read one char from stdin
  2. Concatenate it with the older chars you read
  3. Use realloc to get space for one more char
  4. Do this till you read '\0'

So this is my code:

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


char * readString(void){
    int nChar = 1;
    int cntr = 0;
    char * str = NULL;
    char z;

    while((z = getchar()) != '\0'){
        str = realloc(str, nChar * sizeof(char));
        *(str+cntr) = z;
        cntr++;
        nChar++;
    }

    return str;
}

int main(int argc, char *argv[]) {
    readString();
}

The Problem is that I never read the '\0' char. When I asked my professor he said only that I am also allowed to use getch, fgets and scanf to read one char from the stdin.

Can someone help? I don't have any clue.

I edited my code now. Is this right?

char * readString(void){
    int nChar = 1;
    int cntr = 0;
    char * str = NULL;
    char z;

    while(1){

        if((z = getchar()) != '\n'){
            str = realloc(str, nChar * sizeof(char));
            *(str+cntr) = z;
            cntr++;
            nChar++;
        } else {
            str = realloc(str, nChar * sizeof(char));
            *(str+cntr) = '\0';
            break;
        }
    }

    return str;
}
JamesJr
  • 1
  • 2
  • 5
    You need to enter a nul into the input stream: http://stackoverflow.com/questions/7572801/type-null-character-in-terminal – user877329 May 19 '17 at 08:29
  • 3
    It seems very strange to try to read a null terminator from `stdin`. Are you 100% sure that's what's required? Normally a word or line is read in. –  May 19 '17 at 08:33
  • 2
    `ctrl-@` might do it. – JeremyP May 19 '17 at 08:34
  • 5
    Tell your teacher that it is very unusual to wait for `\0` form stdin. Common patterns are : reading until end of line `\n` or end of stream `EOF`. Beware that `getchar()` returns an `int` not a `char` – Jean-Baptiste Yunès May 19 '17 at 08:35
  • 5
    The `\0` requirement seems dubious. Side note: you should save the result of `realloc()` in a temporary variable, assigning to `str` only if reallocation was successful. The `realloc()` function returns a null pointer in the event of failure, and direct assignment as here leads to data loss and memory leaks. – ad absurdum May 19 '17 at 08:39
  • 1
    Possible duplicate of [How to simulate NUL character from keyboard?](http://stackoverflow.com/questions/9124786/how-to-simulate-nul-character-from-keyboard). But anyway, as commented before, the requirement that the NUL characters must be read from the terminal is dubious. – Jabberwocky May 19 '17 at 08:58
  • 1
    Other sidenotes: 1. Calling `realloc` for each newly added character is not efficient. 2. In your code (supposing the NUL character _can_ be entered) the resulting string is not NUL terminated. – Jabberwocky May 19 '17 at 09:03
  • Is this right now? char * readString(void){ int nChar = 1; int cntr = 0; char * str = NULL; char z; while(1){ if((z = getchar()) != '\n'){ str = realloc(str, nChar * sizeof(char)); *(str+cntr) = z; cntr++; nChar++; } else { str = realloc(str, nChar * sizeof(char)); *(str+cntr) = '\0'; break; } } return str; } – JamesJr May 19 '17 at 09:22
  • 1
    @JamesJr don't post code more than 1 line long in comments, it's unreadable. – Jabberwocky May 19 '17 at 11:25
  • @JamesJr is looks correct, but maintaining the two counters `nChar` and `cntr` which differ always by 1 is awkward. One counter (`cntr`) would be enough and instead of `nchar` write `cntr + 1`. But in this new code input terminates upon `\n` and not `\0` which differs from your initial requirement. – Jabberwocky May 19 '17 at 14:43
  • Suggest `int z; while((z = getchar()) > '\0'){` to stop on both `EOF` and the _null character_. – chux - Reinstate Monica May 19 '17 at 15:23

0 Answers0