Hello all,

I really need some help with this problem. I am writing a program that needs to
count the number of words in a string
display the longest word in that string
display upper and lower cases
and punctuations

I have everything complete except displaying the longest and shortest words. I MUST USE POINTERS and no vectors.

The section causing the most problem is : void longWord
as the very bottom:

Thanks in advance for any help. ;)

The program follows:

#include <iostream.h>
using namespace std;


#include <ctype.h>
#include <cstdlib>
#include <conio.h>
#include <iomanip.h>
#include <string.h>



void input (char*);
void play(char*, char[5000], int&, int&);
void wordCount (char*);
void longWord (char*, int);
void upper (char*);
void numbers (char*);
void punc (char*);


void main()
{
char string [5000] = {'/0'};
char word[5000] = {'/0'};
int x = 0; int n = 0;
input(string);
play(string, word, x, n);
wordCount(string);
longWord(string, x);
upper(string);
numbers (string);
punc (string);
}


void input (char *enter)
{
int len;
cout<<"Enter sentence(s) ";
cin.getline(enter, 5000);
//  cout<<"The string is: " <<enter<<endl;
//  len = strlen(enter);
//  cout<<"The sentence length is: " <<len<<endl;
//    return 0;
}



void play(char *temp, char word[5000], int&x, int &n)
{
int d; int i; int l; int p; int s; int y = 0;
l = strlen(word);
for (i = 0; i < l; i++)
{
d = isdigit(word); p = ispunct(word); s = isspace(word);


if (word == '.' && ispunct(word[i-1]) == 0 && y > 0) n++;
else if (word == '!' && ispunct(word[i-1]) == 0 && y > 0) n++;
else if (word == '?' && ispunct(word[i-1]) == 0 && y > 0) n++;


if (p == 0 && s == 0 && d == 0)
{
*temp *y == word;
y++;
}
else if (ispunct(word[i+1]) == 0 && isspace(word[i+1]) == 0)
{ if (i+1 < l && i > 0 && y > 0) { x++; y = 0; } }
}


}



void wordCount (char *word2)
{
int cnt = 0;


while(*word2 != '\0')
{
while(isspace(*word2))
{
++word2;
}
if(*word2 != '\0')
{
++cnt;
while(!isspace(*word2) && *word2 != '\0')
++word2;
}
}
cout<<"Number of words: "<<cnt<<endl;


}



void upper (char *word2)
{


int caps = 0;
int lower = 0;


int case1 = strlen(word2);


for(int i = 0; i < case1; i++)
{
if(isupper(word2))
caps++;
else if (islower(word2))
lower++;
}
cout<<"\nUpper Case: "<<caps<<endl;
cout<<"Lower Case: "<<lower<<endl;


}



void numbers (char *word2)
{


int num = 0;
int amount = strlen(word2);


for(int i = 0; i < amount; i++)
{
if(isdigit(word2))
num++;


}
cout<<"Digits: "<<num<<endl;



}


void punc (char *sentence)
{


int marks = 0;


int pncs = strlen(sentence);


for(int i = 0; i < pncs; i++)
{
if(ispunct(sentence))
marks++;


}
cout<<"Punctuation: "<<marks<<endl;
system("pause");


}



//HERE IS MY PROBLEM


void longWord (char *temp, int x )
{
//int x;
int i; int l = 0; int m = 0; int n = 0;
for (i = 0; i < x+1; i++)
{
l = int(strlen(temp));
if (l != '/0' > m)
{
m = l;
n = 0;
}
else if (l == m)
n++;
}
cout << endl << "Longest Word(s): ";
for (i = 0; i < x+1; i++)
{
if (int(strlen(temp) == m))
cout << temp;
if (n > 0  && int( strlen(temp)) == m)
{
//cout << " / ";
n--; }
}
cout << endl;
}

Recommended Answers

All 11 Replies

You could use a copy of the original string and break it up into substrings one at a time with a loop and strtok() using space and whatever else you want (punctuation characters, whatever) as a delimiter. Or, if you know the string won't have punctuation characters you could use a istringstream to parse the original string into individual substrings one at a time using the >> operator. Or you could create your own parsing function to isolate each substring by reading single characters one at a time from the original string and if current char isn't a space, or punctuation character or whatever else you want to screen for, add current char to a holding substring variable and if char is a space, or punctuation character or whatever else you want to screen for, then add a null character to the end of the current substring. Either way, if you use a variable to keep track of the length of the longest substring so far and another variable to keep track of the longest substring found so far then you could compare the length of the current substring versus the length of the longest substring found so far and if the current substring is longer, store its length in the length of the longest substring so far variable and the substring itself in the longest substring so far.

Member Avatar for iamthwee

Couldn't you do something like this?

void longWord (char *temp, int x )
{
  int counter = 0;
  int max_word = -1;
  
  int length = int(strlen(temp));
  
  for(int i=0; i<length; i++)
  {
      if(temp[i] !=' ')
      {
          counter++;
      } 
      else if(temp[i]==' ')
      {
          if(counter > max_word)
          {
              max_word = counter;
          }
          counter = 0;
      }
                 
  }  
  cout <<"Longest word:" << max_word;  

}

It depends what your definition of a word is. I've defined it to be separated by whitespace. You might have to change it to ignore punctuation 'n' stuff.

You also have to check for the null character as well '\0'. I'll leave that for you to do.

Hope this helps tee he he.

I have tried this but it displays the entire sentence. I have came at it from all different angles and still the same results: The whole sentence or the first word displays.
Does any one have any suggestions?

Thanks.

Member Avatar for iamthwee

couldn't you do something like this?

#include <iostream>
#include <ctype.h>
//#include <stdlib>
#include <conio.h>
#include <iomanip.h>
#include <string.h>

using namespace std;


int main()
{

  char temp[5000];
  cout<<"Enter sentence(s) ";
  cin.getline(temp, 5000);
  
  int counter = 0;
  int max_word = -1;
  int posi=0;
  
  int length = (strlen(temp));
  temp[length]= ' ';
  
  cout<<length<<endl;
  
  for(int i=0; i<=length; i++)
  {
      if(temp[i] !=' ')
      {
          counter++;
      } 
      else if(temp[i]==' ')
      {
          if(counter > max_word)
          {
              max_word = counter;
              posi=i;
          }
          counter = 0;
         
      }
     
                 
  }  
  cout <<"Longest word:";
   for(int k=posi-max_word; k<posi; k++)
  {cout<<temp[k];}

getchar();
}

Assuming there is only ONE unique longest word?

Thank you ever so much. It works fine. I just need it to display all the longest words of equal length.

Thanks all.

Member Avatar for iamthwee

>I just need it to display all the longest words of equal length.

That's definitely more problematic... Can't think of an easy way to do that especially if you're not allowed to use vectors. Maybe sum1 else can show you.

[IMG]http://img476.imageshack.us/img476/5171/cut20ln.png[/IMG]
Piworld ™
[Tis simple as Pie]

All I can think of is a really annoying set of dynamically-allocated arrays. Or a linked list.

Or, determining what the maximum length is, finding all the words that are that long, creating a loop to store all the words in some data structure...

Probably easiest to do this with a linked list, actually. You'd be able to take care of everything in one go...

Go in word-by-word, store each word in a node, then unlink and delete all the words that are smaller. If you encounter a larger-size word, you can go back, unlink, and delete all the previous words that were 'biggest'.

Make it a loop, and you'll only have to transverse the entire input string once.

yah... linked list wud be better if u have enough knowledge on dat..... otherwise, try with the other one..... it wud be li'l clumsy.... but using good thinking u can decrease the length of the code..... be careful abt the things u r using more than once..... thus u won't have to write seperate function for each of ur q. but seperate functions to solve part of diff. q....... good luck

Member Avatar for iamthwee

Actually, I thought of a simple way to do this.

Loop through the sentence.
Find the length of the longest word.

Now loop through the sentence again.
And check if any other words match the length of the longest word.
Each time you find a match record its index position and print it out.

|       |       |
 V       V       V
|t|h|e| |a|n|d| |w|a|t| |t|o| |I|...

 0 1 2 3 4 5 6 7 8 9 ...
#include <iostream.h>
#include <ctype.h>
//#include <stdlib>
#include <conio.h>
#include <iomanip.h>
#include <string.h>

using namespace std;


int main()
{

  char temp[5000];
  cout<<"Enter sentence(s) ";
  cin.getline(temp, 5000);
  
  int counter = 0;
  int max_word = -1;
  int posi=0;
  
  int length = (strlen(temp));
  temp[length]= ' ';
  
  //cout<<length<<endl;
  
  for(int i=0; i<=length; i++)
  {
      if(temp[i] !=' ')
      {
          counter++;
      } 
      else if(temp[i]==' ')
      {
          if(counter > max_word)
          {
              max_word = counter;
              posi=i;
          }
          counter = 0;
         
      }           
  }  
  cout <<"Longest word(s):";
   
   
   int v_counter=0;
  
   for(int i=0; i<=length; i++)
   {
       if(temp[i] !=' ')
      {
          v_counter++;
      } 
      else if(temp[i]==' ')
      {
          if(v_counter == max_word)
          {
              int p;
              p=i-max_word;
              for(int k=p; k<p+max_word; k++)
              {
                 cout<<temp[k];
              }cout<<",";
             
          }
          v_counter = 0;  
      }              
  } 

getchar();
}

Hope this helps

[IMG]http://img476.imageshack.us/img476/5171/cut20ln.png[/IMG]
Piworld ™
[Tis simple as Pie]

You all are great!!! Thank you for all of your help.
I will provide any assistance to others that I can.

Here is the most simple and easy to to understand code to find longest word in a sentence:

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

void main()
{
clrscr();
int c=0,k=0,sp=1,max,j,len=0,b[20],x=0;

char arr[50];
cout<<"Enter a sentence";
gets(arr);
for(len=0 ;arr[len]!=NULL ;len++);
cout<<"Entered string is :";
for(int i=0 ;i<len ;i++)
{
cout<<arr;
}
cout<<"\n"<<"Len ="<<len;
//Calculating spaces
for(i=0 ;i<len ;i++)
{
if(arr == ' ')
{
c++;
}
}
cout<<"\nSpaces ="<<c<<"\n";
c=0;
//Finding position of occurence of first space
for(i=0 ;i<len ;i++)
{
if(arr==' ')
{
break;
}
c++;
}
cout<<"Position of first space is "<<c+1<<"\n";
i=0;
//Storing length of each word in an array
for( i=0 ;i<len;i++)
{
if((arr ==' ') && (sp==1))
{
for(j=i-1 ;j>=0 ;j--)
{
k++;
}
b[x]=k; x++;
k=0 ,sp++;
}

if((arr ==' ') && (sp!=1) && (i!=c))
{
for(j=i-1 ;arr[j]!=' ' ; j--)
{
k++;
}
b[x]=k; x++;
k=0; sp++;
}
}
//Storing the length of last word in the array
while(arr !=' ')
{
k++;
i--;
}

b[x]=k-1;
cout<<"Length of each word ";
for(i=0 ;i<=x;i++)
{
cout<<b<<" ";
}
max=b[0];k=0,sp=0;
//Finding max length
for(i=0 ;i<=x ;i++)
{
if(max<b)
{
max=b;
k=i;
}
}
cout<<"\nLargest word is after "<<k<<" space\n";
cout<<"Largest word is ";
//Comparing array from string and printing the largest word
for(i=0 ;i<len ;i++)
{
if(arr==' ')
{
sp++;
}
if(sp==k)
{
if(sp==0)
{
for(j=i ;arr[j]!=' ' ;j++)
{
if(arr[j]==NULL)
{
break;
}
cout<<arr[j];
}
cout<<"\n"; sp++;
}
else
{
for(j=i+1 ;arr[j]!=' ' ;j++)
{
if(arr[j]==NULL)
{
break;
}
cout<<arr[j];
}
cout<<"\n"; sp++;
}
}
}
getch();
}

//I have done it step bt step for beginners to understand easily.

Thank you if u liked!!:)

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.