C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 18th, 2013, 08:27 AM
FufuAlex FufuAlex is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 3 FufuAlex User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 44 m 42 sec
Reputation Power: 0
Need help with my code

Hi, I'm coding a program which count the number of words in the string.
Example: asfasf[space][space]asfkuef[space]a[space]
Output:3 words, 14 letters, 4 space

But I'm getting error with the code. someone help me out?

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

void main()
{
	char str[50];
	int words = 0, len = 0, i;
	clrscr();
	printf("\n--------------------------------------------------------");
	printf("\n\nPROGRAM TO COUNT THE NUMBER OF WORDS IN A GIVEN STRING");
	printf("\n\n--------------------------------------------------------");
	printf("\n\n\t ENTER A STRING...: ");
	gets(str);
	while(str[len]!='\0')
		len++;
	len--;
	for(i=0;i<=len;i++)
	{
		if((str[i] == ' ' && str[i+1] != ' ') || i == len)
			words++;
	}
	printf("\n\t NUMBER OF WORDS IN THE ABOVE SENTENCE IS...: %d", words);
	printf("\n\n-------------------------------------------------------");
	getch();
}

Reply With Quote
  #2  
Old January 18th, 2013, 08:55 AM
FufuAlex FufuAlex is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 3 FufuAlex User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 44 m 42 sec
Reputation Power: 0
Hey guy is ok. i solve it by changing void to int and remove clrscr();.

btw how can i count the number of space?

Reply With Quote
  #3  
Old January 18th, 2013, 09:28 AM
jasondj jasondj is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2010
Posts: 92 jasondj User rank is Sergeant Major (2000 - 5000 Reputation Level)jasondj User rank is Sergeant Major (2000 - 5000 Reputation Level)jasondj User rank is Sergeant Major (2000 - 5000 Reputation Level)jasondj User rank is Sergeant Major (2000 - 5000 Reputation Level)jasondj User rank is Sergeant Major (2000 - 5000 Reputation Level)jasondj User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 5 h 52 m 39 sec
Reputation Power: 24
Check out the strtok function. With a little creativity, you should be able to simplify your program significantly.

http://www.cplusplus.com/reference/cstring/strtok/

Reply With Quote
  #4  
Old January 18th, 2013, 10:07 AM
FufuAlex FufuAlex is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 3 FufuAlex User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 44 m 42 sec
Reputation Power: 0
Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>

int main()
{
	char str[50];
	int words = 0, len = 0, i, number;
//	clrscr();
	printf("\n--------------------------------------------------------------------------");
	printf("\n\nPROGRAM TO COUNT THE NUMBER OF WORDS, STRING, SPACE IN A GIVEN STRING");
	printf("\n\n--------------------------------------------------------------------------");
	printf("\n\n\t ENTER A STRING...: ");
	gets(str);
	number = strlen(str);
	while(str[len]!='\0')
		len++;
//	len--;
	for(i=0;i<=len;i++)
	{
		if((str[i] == ' ' && str[i+1] != ' ') || i == len)
			words++;	
	}
	printf("\n\t NUMBER OF WORDS IN THE ABOVE SENTENCE IS...: %d", words);
	printf("\n\n-------------------------------------------------------");
	printf("\n\t NUMBER OF STRING IN THE ABOVE SENTENCE IS...: %u", number);
	printf("\n\n-------------------------------------------------------");
	  
	getch();
	return 0;
}

I'm able to count the number of string. but I'm still confuse with the space counting? would you help me out on the code?

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Need help with my code

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap