7 Replies - 11778 Views - Last Post: 22 January 2010 - 01:14 PM Rate Topic: -----

#1 neo_rohan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-January 10

C program 2 count number of words in a line

Posted 08 January 2010 - 10:59 PM

I have done this program
my task is to remove all multiple spaces & tabs at start , in between & at the end also
Cn u please give me any hint about wht is wrong in my program ..



#include<stdio.h>
#include<conio.h>

void main()
{
	int i,c=0;
	char line[50];
	clrscr();
	printf("Enter any line\n");
	gets(line);
	if(line[0]==' ')
	c=0;
	else
	c=1;
	for(i=0;line[i]!='\0';i++)
	{
	if(line[i]==' ' && line[i+1] != ' ' && line[i] != '\t' ) // check the next character with [i+1]
	c++;
	}
	printf("No.of word in a line=%d",c);
	scanf("%d",&c);
	getch();
}
 





Thanking you ..

Is This A Good Question/Topic? 0
  • +

Replies To: C program 2 count number of words in a line

#2 athlon32  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 363
  • Joined: 20-August 08

Re: C program 2 count number of words in a line

Posted 08 January 2010 - 11:22 PM

== EDIT: post got screwed ==

This post has been edited by athlon32: 08 January 2010 - 11:29 PM

Was This Post Helpful? 0
  • +
  • -

#3 neo_rohan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-January 10

Re: C program 2 count number of words in a line

Posted 08 January 2010 - 11:27 PM

View Postathlon32, on 8 Jan, 2010 - 10:22 PM, said:

What seems to be the problem? Post any error messages you receive. Help us help you :D



CAn anyone plz tell me how to remove spaces at the end of the line ...
there is no error in the program except i cant remove the end spaces
Was This Post Helpful? 0
  • +
  • -

#4 GenHornet18  Icon User is offline

  • Darken the Radar

Reputation: 36
  • View blog
  • Posts: 629
  • Joined: 19-October 09

Re: C program 2 count number of words in a line

Posted 08 January 2010 - 11:50 PM

I don't understand, you are programming a console application and you don't expect there to be blank spaces at the end of the line? Or you referring to blank spaces in your array?

EDIT: int main() {return(0);}

This post has been edited by GenHornet18: 08 January 2010 - 11:52 PM

Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6112
  • View blog
  • Posts: 23,672
  • Joined: 23-August 08

Re: C program 2 count number of words in a line

Posted 09 January 2010 - 12:38 AM

So you're saying you found some code, and now you want us to fix it to do what you need it to do?
Was This Post Helpful? 0
  • +
  • -

#6 crazyloveguru  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 22-January 10

Re: C program 2 count number of words in a line

Posted 22 January 2010 - 09:32 AM

this is 100 % useful to you because this will remove all the blank spaces or tab in the entered line.

#include<stdio.h>
#include<conio.h>


void main()
{
    int i,c=0;
    char line[50];
    clrscr();

    printf("Enter any line\n");
    gets(line);

    if(line[0]==' '||line[0]=='\t')
	c=0;
    else
	c=1;

    for(i=0;line[i]!='\0';i++)
    {
	    if(line[i]==' ' && line[i+1] != ' ' && line[i] != '\t' ) // check the next character with [i+1]
		c++;

	    else if(line[i]=='\t' && line[i+1] != '\t' && line[i] != ' ' ) // check the next character with [i+1]
		c++;
    }

    printf("No.of word in a line=%d",c);
    getch();
}


*** MOD EDIT: Added code tags. Please :code: ***

This post has been edited by JackOfAllTrades: 22 January 2010 - 10:03 AM

Was This Post Helpful? 0
  • +
  • -

#7 algotechie  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 14
  • Joined: 29-November 09

Re: C program 2 count number of words in a line

Posted 22 January 2010 - 12:42 PM

This is in reply to the first post. As far as I could understand it, probably you wanna treat multiple spaces or a tab or multiple tabs or a combination of these white characters as one single space, and that way wanna count the number of words. After getting the "line" input from the user, try this code:

c=0;
i=0;
while (1)
{
while (line[i]&&((line[i]==' ')||(line[i]=='\t')))
++i; // To iron out the spaces and tabs
if (line[i])
++c; // To increment the word-count when a non-space and non-tab character is found...
else
break; // ... (other than NULL of course)
while (line[i]&&(line[i]!=' ')&&(line[i]!='\t'))
++i; // To skip over the rest of the non-space and non-tab characters in the word
}


Hope that was helpful.
Was This Post Helpful? 0
  • +
  • -

#8 Paul-  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 61
  • View blog
  • Posts: 260
  • Joined: 11-December 09

Re: C program 2 count number of words in a line

Posted 22 January 2010 - 01:14 PM

View Postneo_rohan, on 8 Jan, 2010 - 09:59 PM, said:

I have done this program
my task is to remove all multiple spaces & tabs at start , in between & at the end also


You could use sscanf() to read individual words from the line string.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1