LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-22-2012, 09:01 AM   #1
edu_linux
LQ Newbie
 
Registered: Jun 2012
Posts: 1

Rep: Reputation: Disabled
c++ pgm to return a list which contain each word and no of occurence in a sentence


im feeling little difficult to implement this even though it is easier please some one help me.

some what i managed to count no of words,

Code:
#include<iostream>
#include<vector>
int parse_sentence(char sent[]);
using namespace std;
 int  main()
{
char sentence[]="i am rama friend";
int word_count=parse_sentence(sentence);
cout<<"no of words occurance"<<word_count;
return 0;
}

int parse_sentence(char sent[])
{
 int count=0;  
for(int i=0;sent[i]='\0';i++)
  {
   if(sent[i]==' ')
    {
   
     count++;

    }
  }
return count;
}
 
Old 06-22-2012, 09:28 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
The most obvious error in that code is
Quote:
Originally Posted by edu_linux View Post
Code:
for(int i=0;sent[i]='\0';i++)
A single = means assignment, even when used where it looks like an equality test.
An equality test requires ==
But the middle value of a for is a continue condition, not a stopping condition, so I think you need !=

For the more complicated project requested in the title of your post: You shouldn't expect us to do your homework for you.

I expect the point of the assignment is to learn something about associative containers, such as std::map. If you have learned something about that already, then you should be able to figure out how to apply that knowledge to this problem. If you haven't learned about std::map yet, now is a good time to learn that.

You also should know how to use std::string. Working with C strings (char const*) is practical in C++, but in a lot of uses (such as the key for an associative container) a std::string is a lot easier to use.

Last edited by johnsfine; 06-22-2012 at 09:37 AM.
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Word list generation (all combinations) from a list of characters llattan Linux - Newbie 1 04-07-2009 07:41 PM
Capitalise Only First Letter Of Each Word In A Sentence onesikgypo Programming 2 02-15-2009 11:41 AM
variable length string using GD (word wrap, carriage return, word/character count)? frieza Programming 1 02-14-2009 05:21 PM
Hi everybody!!! Im a new user at this list.... soon I return with some questions... luizgouvea LinuxQuestions.org Member Intro 5 06-03-2007 10:46 AM
function won ' t return character array word Linh Programming 1 07-31-2003 06:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration