Hello im working on a C program that counts the number of tabs, blanks and newlines. Here is my code:

#include <stdio.h>
#define MAXLENGTH 500

/*	This program accepts a string as input.
*	It should then count blanks, tabs and newlines.
*	It should print out the number of each seperately on a new line.
*/

int main()
{
	char s[MAXLENGTH]; /*Array which is the string*/
	char *sp = &s[0]; /*Pointer for address of first element in s[]*/
	char c; /*Contains the current character being checked */
	int i; /*Used as an incrementor to traverse the array*/
	int i_bl, i_tb, i_nl; /*Stores the number of blanks, tabs and newlines in these counting variables*/
	

	i_bl = i_tb = i_nl = 0; /*Assigns the initial value of the counting variables*/
	scanf("%s", sp); 
	printf("%s\n", s); /*Testing the string was infact inputted
	for (i = 0; c != '\0'; i++)/*Traverses the character string until EOF*/
	{
		c = s[i]; /*Stores the current character*/
		switch(c) /*For blanks, tabs and new lines this incremements the appropriate counting variable*/
		{
			case ' ': i_bl++; break;
			case '\t': i_tb++; break;
			case '\n': i_nl++; break;
			default: break;
		}
	
	}

	printf("%d\n%d\n%d\n", i_bl, i_tb, i_nl);
	
	return 0;
}

My problem is that it just isn't counting. The output always reads 0 0 0.

Also when I input 'Hello ', it doesnt print any of the blank spaces after hello.

So yeah, help on making it work and advice on a better way to input strings please.

Salem commented: Nice! indented code and code tags on the first post, a rare sight indeed! +19
jephthah commented: and a cool screen name, too. +6
tux4life commented: if ( nice_post ) rep++; :) +8

What a fantastic first post!
Code tags and nicely indented code - top notch stuff.

> The output always reads 0 0 0.
scanf("%s") uses all white space as a delimiter, so no spaces etc will ever appear in your string.

Try fgets( s, sizeof(s), stdin ); instead.

commented: Simple and effective advise. +8
commented: you can lead a horse to water... +6

You might wanna see that in LINE # 20 & 21, you messed up the comments. It should be like this

printf("%s\n", s); /*Testing the string was infact inputted*/
for (i = 0; c != '\0'; i++)/*Traverses the character string until EOF*/

I was just thinking, can we give more than one newline during input using stdin

Hmmm it still isnt working... outputs 0 0 0 still.

I was just about to mention that fgets() returns one line at a time...

fgets() will stop when it reaches the end of a line, in which case str will be terminated with a newline. If fgets() reaches num - 1 characters or encounters the EOF, str will be null-terminated

do I have to do something weird like loop it and append each subsequent line to the end of of the current string using a pointer?

::edit:: Oh and that comment isnt in the code.

Ok ive just fixed it and it works perfectly :)

Just decided to use the getchar(c) function to take the input, haha made it so easy.

One small question however is why must i use...

while ( (c = getchar() != EOF)

As opposed to...

while ( (c = getchar() != '\0')

Ok ive just fixed it and it works perfectly :)

Just decided to use the getchar(c) function to take the input, haha made it so easy.

One small question however is why must i use...

while ( (c = getchar() != EOF)

As opposed to...

while ( (c = getchar() != '\0')

::edit:: OHWAIT!!

Sorry im still having a rather annoying problem!
Whenever I run my code using

gcc -Wall -W -ansi -pedantic blah
./a.out

It never stops taking input and I still don't know a shortcut key to abort from the program.

Here is the code.

#include <stdio.h>
#define MAXLENGTH 500

/*	This program accepts a string as input.
*	It should then count blanks, tabs and newlines.
*	It should print out the number of each seperately on a new line.
*/

int main()
{	
	char c; /*Contains the current character being checked */
	int i_bl, i_tb, i_nl; /*Stores the number of blanks, tabs and newlines in these counting variables*/
	
	i_bl = i_tb = i_nl = 0; /*Assigns the initial value of the counting variables*/

	while ( (c = getchar()) != EOF)/*Traverses the character string until EOF*/
	{
		switch(c) /*For blanks, tabs and new lines this incremements the appropriate counting variable*/
		{
			case ' ': i_bl++; break;
			case '\t': i_tb++; break;
			case '\n': i_nl++; break;
			default: break;
		}
	
	}

	printf("%d\n%d\n%d\n", i_bl, i_tb, i_nl);
	
	return 0;
}

I think you will have to use fgets only.

im still having a rather annoying problem! .... It never stops taking input and I still don't know a shortcut key to abort from the program.

because you're using getchar() to return into a char type, when it's return type is int. getchar() is a clunky function, and not well suited for getting strings. IMO, you should not use it. if you insist on using it, anyhow, then you need to use it with an int, not a char.

I say, you should use fgets() like Salem showed you.


.

Set value of i to zero int i = 0; and put fgets(s,sizeof(s),stdin) instead of using getchar() This program is running fine in my compiler with those two modifications

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.