LinuxQuestions.org
Review your favorite Linux distribution.
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 10-05-2010, 10:25 PM   #1
Tacitus
LQ Newbie
 
Registered: Sep 2010
Posts: 2

Rep: Reputation: 0
Unhappy finding longest and shortest word in a string


hi, I'm a bit of a noob, but I am trying to finish writing a program that computes the longest and shortest word of a string in c++ among other things. so far, I have this function for the longest word-
int findLongest (string y)
{
int j=y.length();
int i=0;
int number=0,newnumber=0;

for (i=0; i<=j;++i)
{
if (y.at(i)==32)
{number=0;}
else
{number++;}
if (newnumber<number)
{newnumber=number;}//<<i suspect
}

however, whenever I try to run it, I get an out of bounds error, and I can't figure out how to stop it from "running off the end of the loop".
any thoughts of how to fix this and how to convert it to get the shortest word would be greatly appreciated, been working on making the program for three days when it shoulda been done in two 0.o
 
Old 10-05-2010, 11:54 PM   #2
basheer
Member
 
Registered: Mar 2009
Location: Bangalore, India
Distribution: CentOS6.5, CentOS7, Ubuntu14.04
Posts: 182

Rep: Reputation: 29
Your logic itself seems to be wrong.
There are a lot of loopholes.
First of all you are not saving the longest and the shortest words.

Quote:
if (y.at(i)==32)
{number=0;}
In the above if statement you have to save the words. You should also have variables for saving the longest word count and the shortest word count.

You must be learning c++ right?
Get a good book. There are a lot of books available for free on the net.
Hope I'm not offending you.
 
Old 10-06-2010, 10:58 AM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
because arrays start counting at 0 (int i=0) they must stop at the length minus one or (j-1). This is done by using the i<j rather than i<=j

Hope that helps you a little.
 
  


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
[SOLVED] sed - Rule: "leftmost match, longest possible string, zero also matches."? quanta Programming 5 10-18-2009 08:37 AM
grep string with space (2 word string) casperdaghost Linux - Newbie 7 08-24-2009 02:11 AM
variable length string using GD (word wrap, carriage return, word/character count)? frieza Programming 1 02-14-2009 05:21 PM
find word between string djcham Programming 8 07-11-2008 04:37 AM
select the longest string of each unique path in a sorted text file powah Programming 5 03-01-2007 11:16 AM

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

All times are GMT -5. The time now is 08:57 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