Fred Vann
Fred Vann

Reputation: 19

How do I read the number of characters from a input in C?

I am trying to read the number of characters including, the spaces. I use the scanf function to check for chars using %c. Also on a side note, how would I go about storing the input into an array?

#include <stdio.h>

int main(void) {
  char n, count= 0;

  while (scanf("%c", &n) != EOF) {
    count = count+1;
  }  

  printf("%d characters in your input \n", count);

  return 0;
}

When I test input (with spaces) such as abcdefg it doesn't print anything.

Upvotes: 2

Views: 2297

Answers (4)

Avinash Sargara
Avinash Sargara

Reputation: 1

I don't know what you asked for, but I was searching for a program where a user is entering a string and when he/she press enter after completing his string, the program stops and tell the number of character in that string he/she entered. This one worked for me.

#include <stdio.h>
int main(void){
char n, count= 0;

while (scanf("%c", &n) && n != '\n') 
{
count = count+1;
}  

printf("%d characters in your input \n", count);

return 0;
}

Upvotes: 0

Ravi
Ravi

Reputation: 81

Defining a MAX_CHAR and checking that in loop would protect you against invalid memory write. Remember that last byte of an array should be left for '\0', if you want to print or use the char array.

#include <stdio.h>
#define MAX_CHAR 100

int main(void) {

char n[MAX_CHAR]={0}, count= 0;

while((count!=MAX_CHAR-1)&&(scanf("%c",&n[count])==1))
{
    if((n[count]=='\n')){
        n[count]=0;
        break;
    }
    count++;
}

printf("%d characters in your input [%s]\n", count, n);

return 0;
}

Upvotes: 2

Joao Lima
Joao Lima

Reputation: 31

scanf does return EOF when it reaches the end of the file. But in order for you to see that happening, you should give your program a file input when you call it like this:

./a.out < input.txt

Inside input.txt you could put any text you want. But if you want to work in the command line, you should read until you find a \n

#include <stdio.h>

int main(void) {
  char n, count = 0;
  scanf("%c", &n);
  while (n != '\n') {
    count = count+1;
    scanf("%c", &n);
  }  

  printf("%d characters in your input \n", count);

  return 0;
}

If you want to store the input in an array, you must know the size of the input (or at least the maximum size possible)

#include <stdio.h>

int main(void) {
  char n, count = 0;
  char input[100]; //the max input size, in this case, is 100
  scanf("%c", &n);
  while (n != '\n') {
    scanf("%c", &n);
    input[count] = n; //using count as the index before incrementing
    count = count+1;
  }  

  printf("%d characters in your input \n", count);

  return 0;
}

Furthermore, if don't know the size or max size of the input, you'd have to dynamically change the size of the input array. But I think that would be a little advanced for you right now.

Upvotes: 1

Login
Login

Reputation: 105

Your printf doesn't print anything because runtime doesn't reach to it. Your code looping for ever in while loop

  while (scanf("%c", &n) != EOF) {
    count = count+1;
  }

because scanf won't return EOF in this case

Upvotes: -1

Related Questions