Problems with a character counting program in C on online compilers - c

I'm a newcomer to programming so please forgive me for my stupid questions. I ran the following code on repl.it and JDoodle respectively.
#include<stdio.h>
int main()
{
int nc;
nc = 0;
while(getchar() != EOF)
nc = nc + 1;
printf("%d\n", nc);
}
On JDoodle, the results are always x+1 where x is the number I expected. For example, if I input 123123, the result is 7. Besides, if I leave the input empty, JDoodle tells me that Your Program may have a endless loop. Why these happen?
On repl.it, after I type the input on the right side of the screen and press Enter, there is no response. However the input copying program
int c;
c = getchar();
while(c != EOF){
putchar(c);
c = getchar();
}
works well on repl.it. Why the character counting program cannot work on repl.it? I will appreciate any help with these situations.

the following proposed code:
corrects the problem discussed in the comments to the question.
The differences between the two web pages is of no consequence to the problem seen by the OP.
And now the proposed code:
#include <stdio.h>
int main( void )
{
int nc = 0;
int ch; // the character input from stdin
while( (ch = getchar()) != EOF && '\n' != ch)
nc = nc + 1;
printf("%d\n", nc);
}

Related

Input does not exit in c, thus output does not work

I am just learning c, and I am using Linux terminal. I have written the following simple code, but when I type the inputs, the file does not exit and thus does not count the number of characters. Can anyone help me with it? I have tried other input codes as well. It's the same with all my input related codes. What am I doing wrong? Please help.
main()
{
/* count characters in input */
printf("Type some characters and the program will count the number of characters: ");
int c = getchar();
while(c!=EOF && c!= '\n')
++c;
printf("Number of characters typed: %1d\n", c);
}
So to take notice of the helpful comments;
#include <stdio.h>
int main(){
int c;
int count = 0;
while((c=getchar()) != '\n' && c != EOF)
count++;
printf("%d\n", count);
};
This code works as expected.

Character Count in C

The folllowing C Program is to calculate the character Count .
#include <stdio.h >
int main()
{
int nc = 0;
while (getchar() != EOF)
{
++nc;
printf("%d\n", nc);
}
return 0;
}
When I Enter a character , for example 'y' in the terminal , The output returns as follows
1
2
How does this calculation happens and why 2 is in the output?
I suppose you didn't know but when you press enter you just insert a newline character or '\n'. If you want to get the correct result ignore the newline character or just decrease the nc by one.
#include <stdio.h>
int main()
{
int nc = 0;
while (getchar() != EOF)
{
++nc;
printf("Character count is:%d\n", nc - 1);
}
return 0;
}
Even better code:
#include <stdio.h>
int main()
{
int nc = 0;
for(;;)
{
do
++nc;
while (getchar() != '\n');
printf("Character count is:%d\n", nc - 1);
nc = 0;
}
}
The updated code will reset your counter back to 0.
You ENTERed "a character". A y and a new line character. That's 2.
Because you entered two character. One is y and another one is \n(newline) character.
Hence you get the output 1 and 2.
If you want to count only the visible bytes, you can use the isprint function, which returns whether a byte is printable or the space character. It goes like this:
#include <ctype.h>
#include <stdio.h>
int main()
{
int nc = 0;
int ch;
while((ch = getchar()) != EOF)
{
if (isprint(ch) && ch != ' ')
++nc;
printf("Character count after reading '%c' is %d.\n",ch, nc);
}
return 0;
}
Note that since in C, a char is not a Unicode character but often just a byte, this program counts some characters as 2 or more bytes, for example emojis, Cyrillic letters, Chinese ideographs.
When you hit enter it's considered a character

Code prints out correctly with a leading space.

I'm new to C programming and I'm having probably a simple issue.
I've looked all over the site looking for an answer to this but my no luck.
So my program runs fine if at the end of the input file there is a space. For example running the program from command line in unix
./program-name < file.txt
if the file.txt is
//with a space whith | | being a space.
The Cat Ran.| |
I get the correct output 1 1 1
if it is
The Cat Ran.(With no space I get 1 1)
//I have to use C for this program.
#include <stdio.h>
int main(void){
int i=0;
char c;
int NOV=0;
while( (c=getchar())!=EOF && c !='\n' && c !=10 ){
if( c=='a'||c=='e'||c=='i'||c=='o'||c=='u' ){
NOV++;
}
if(c==' '){
printf("%d ",NOV);
NOV=0;
}
}
printf("\n");
return 0;
}
Any Help is appreciated.
The trouble is that when you get EOF without a space, you exit the loop, and nothing after the loop prints the number of vowels.
Add if (NOV != 0) printf("%d", NOV); after the loop, perhaps.
Separately, your use of 10 is odd; '\n' is control-J or 10. You might be thinking of control-M, aka carriage return or '\r'.
Also, as Blue Pixy pointed out in a comment, you should always use int c; to receive the return from getchar() or getc() of fgetc(). The functions return an int, which may be any value that fits in an unsigned char or EOF, a negative value. You run into problems if you use char c instead.
Your program should read like this (a lot of useless clutter has been removed) :
int main(void)
{
int c;
int nov = 0;
while( (c = getchar()) != EOF && c != '\n')
{
if( c=='a'||c=='e'||c=='i'||c=='o'||c=='u' )
nov++;
}
printf("%d\n", nov) ;
return 0;
}

Counting input characters in C

I have just started learning C, been reading a C textbook by Keringhan and Ritchie. There was this example in the textbook, counting characters from user input. Here's the code:
#include <stdio.h>
main()
{
long nc;
nc = 0;
while(getchar() != EOF) {
if (getchar() != 'q')
++nc;
else
break;
}
printf("%ld\n", nc);
}
The problem is, when I execute the code, if I input only one character per line, when I input "q" to break, it doesn't do so. I have to type some word per line, only after that it will break the loop. Also, it only counts the half of the characters of the word. I.e. if I input
a
b
russia
it will only print '5' as final result.
Could you please explain to me why is this happening?
This works, but only when you finish off with an Enter. So, this will count the characters until the first "q" appears. That is just how getchar() and getc(stdin) work.
#include <stdio.h>
int main() {
char c = 0;
long count = 0;
short int count_linebreak = 1; // or 0
while((c = getchar()) != EOF) {
if(c != 'q' && (count_linebreak || (!count_linebreak && c != '\n'))) {
++count;
}else if(c == 'q') {
printf("Quit\n");
break;
}
}
printf("Count: %ld\n",count);
return 0;
}
A StackOverflow question about reading stdin before enter
C read stdin buffer before it is submit

k and r line counting 1.5.3 not working?

I'm having trouble with k&r 1.5.3. Obviously I'm a complete beginner. Below is the code exactly from the book and exactly as I typed it. It compiled fine and runs. It returns characters but just never prints the line count. I'm using ssh into a Ubuntu machine. Can the key on my wife's mac not be interpreted as '\n'?
#include <stdio.h>
/*count lines in input*/
main()
{
int c, n1;
n1 = 0;
while ((c = getchar()) != EOF)
if (c == '\n')
++n1;
printf("%d\n", n1);
}
Correct. Mac uses \r as line ending: http://en.wikipedia.org/wiki/Newline
Update your code like this:
#include <stdio.h>
/*count lines in input*/
main()
{
int c, n1;
n1 = 0;
while ((c = getchar()) != EOF)
if (c == '\r') /* use \r for Macs */
++n1;
printf("%d\n", n1);
}
However
When I try to do the same, I have to Ctrl-D to enter an EOF and trigger the program to print the line count.

Resources