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;
}