1

I wanna know how would you scan a string on unknown length? Suppose I ask for someone's full name and that includes spaces too and just want to scan what the input is and print it. I read a few answers about this but all included a long function using malloc/realloc. Is there a way I could ask for the input and just print it in the main function itself?

#include <stdio.h>

int main(void)
{
    char name[100][100];
    int i,n;

    printf("\nEnter The Limit : ");
    scanf("%d", &n);

    for(i=0; i<n; i++)
        scanf("%s\n"),name[i]);

    for(i=0; i<n; i++)
        printf("\n%s",name[i]);
}

This is as far as I can think of, I can only put a limit to the number of characters you enter, is there any other better way to do it? Could I use strlen here if it's possible?

I read this question How can I read a input string on unknown length in c? The answers here mention using a different function, I want to know if there is a simpler way to do it ? C programmers often make big programs where they ask for the name of the person including fullname? Do all of you use the large function as mentioned in the answered question or is there a simpler way?

Thanks for answering!

era5tone
  • 579
  • 5
  • 14
  • 4
    Does this answer your question? [How can I read an input string of unknown length?](https://stackoverflow.com/questions/16870485/how-can-i-read-an-input-string-of-unknown-length) – Simon Doppler Feb 02 '20 at 14:56
  • @SimonDoppler I read the answer, the used file pointer to open a file and then used a bigger function, Is there a smaller way where I could just write a simple program to scan a name and print it ? – era5tone Feb 02 '20 at 14:58
  • @Nina I don't understand very technical terms yet but does carriage hit means '\0' ? If so after a single word, the compiler encounters '\0' and I also want to know what other words are present next to it too? – era5tone Feb 02 '20 at 15:01
  • The 1-liner is [this fgets answer](https://stackoverflow.com/a/16870717/3700414). Note it assumes 255 chars. You can make that 10000 chars if you like. But if you really want completely arbitrary lengths known only at runtime some kind of malloc/realloc is inevitable (in C) – Rusi Feb 02 '20 at 15:06
  • @user3121023 Could you please write the whole code please, fgets is used with the file pointer but here we are just reading the input on the screen? – era5tone Feb 02 '20 at 15:07
  • 1
    There is not a "simpler way" than using a function. Why would you not use a function in the first place? You'd have to duplicate the code every time you need to read a string from input again. – Marco Bonelli Feb 02 '20 at 15:41

2 Answers2

2

Use fgets for input. Unless stdin has been redirected, fgets will read your input. Because of the array sizes, fgets will read up to 99 characters or a newline.
For integers, read a line with fgets and parse with sscanf or strtol.
If fgets fails, it will return NULL.
sscanf will return the number of items that were successfully scanned. In this case 1 or 0 and can return EOF.

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

int main(void)
{
    char name[100][100] = { ""};
    char line[100] = "";
    int i = 0;
    unsigned int n = 100;
    int result = 100;

    do {
        printf("\nEnter The Limit : ");
        fflush ( stdout);
        if ( ! fgets ( line, sizeof line, stdin)) {
            fprintf ( stderr, "fgets EOF\n");
            exit ( EXIT_FAILURE);
        }
        result = sscanf(line,"%u", &n);
    } while ( 1 != result || n >= 100);

    for(i=0; i<n; i++) {
        printf("\nEnter name[%d] : ", i);
        fflush ( stdout);
        if ( ! fgets ( name[i], sizeof name[i], stdin)) {
            fprintf ( stderr, "fgets EOF\n");
            exit ( EXIT_FAILURE);
        }
    }
    for(i=0; i<n; i++) {
        printf("\n%s",name[i]);
    }
    return 0;
}
user3121023
  • 8,181
  • 5
  • 18
  • 16
2

lets say you have a character pointer, a simple way would be to loop using a while until you find the end of the string.

This below regards scanning one name

#define MAXLENGTH 255

int n,i=0;
char* fauxString=(char*)calloc(MAXLENGTH, sizeof(char));
//Make initial allocation of max amount of characters

fgets(fauxString, MAXLENGTH, stdin);
// using fgets to get the complete line from input buffer using stdin

while (fauxString[i]!='\0'){i++;}
// finding length of the input

n=i+1;
//storing length in n

fauxString=(char*)realloc(fauxString,n*sizeof(char*));
// reallocating to size n of actual "string"

printf(fauxString);
Ario Amin
  • 91
  • 7
  • This is without any checks for validity and names such as "3#(/DHK::_djj" would still be accepted. If you want additional checks you can check within the while loop if a given character is using accepted characters. Use this for reference for ascii utf-8 https://theasciicode.com.ar/ If not, then you can find similar tables for other character maps outside of ascii standard. – Ario Amin Feb 02 '20 at 16:16
  • Yes you are correct, user3121023 sry. Missed that and will correct it! – Ario Amin Feb 02 '20 at 16:17