Can someone help me finish this program? I\'m done with the printing of the word
ID: 3685761 • Letter: C
Question
Can someone help me finish this program? I'm done with the printing of the words in the normal, reverse and backwards. But can someone help me write the code the code to find the number of letters in each word, the words read, the length of the longest and shortest word and the number of times each word length occured? I can't use anything from any of the libraries. They all have to be in their own functions like the rest.
#include
using namespace std;
char ** getWords(char *line);
int normalPrint(char **words);
void reversePrint(char **words, int index);
int getLength(char *word);
void backwardsPrint(char ** words);
int main()
{
char *line = new char[256];
cin.getline(line, 256);
char **words = getWords(line);
int num_words = normalPrint(words);
reversePrint(words, num_words - 1);
backwardsPrint(words);
system("pause");
return 0;
}
char ** getWords(char *line)
{
char *words[256];
int i=0;
int word_count = 0, word_index=0;
for(i = 0; i < 256; ++i)
{
words[i] = 0;
}
i=0;
words[word_count] = new char[256];
while(line[i] != '')
{
if(line[i] == ' ')
{
words[word_count][word_index] = '';
word_count = word_count + 1;
words[word_count] = new char[64];
word_index = 0;
}
else
{
words[word_count][word_index] = line[i];
word_index = word_index + 1;
}
++i;
}
return words;
}
int normalPrint(char **words)
{
int index = 0;
cout << "Normal Print: " << endl;
while(words[index] != 0)
{
cout << words[index] << " ";
++index;
}
cout << endl;
return index;
}
void reversePrint(char **words, int index)
{
cout << "Reverse Order: " << endl;
while(index >= 0)
{
cout << words[index] << " ";
--index;
}
cout << endl;
}
int getLength(char *word)
{
int index = 0;
while(word[index] != '')
{
++index;
}
return index;
}
void backwardsPrint(char ** words)
{
int index = 0;
cout << "Backwords words: " << endl;
while(words[index] != 0)
{
for(int i = getLength(words[index])-1; i>=0; --i)
{
cout << words[index][i];
}
cout << endl;
++index;
}
}
Explanation / Answer
Answer -
#include <iostream>
using namespace std;
char ** getWords(char *line);
int normalPrint(char **words);
void reversePrint(char **words, int index);
int getLength(char *word);
void backwardsPrint(char ** words);
void lettercount(char ** words);
void smallestword(int n_words,int wordlen1[]);
void longestword(int n_words,int wordlen1[]);
void lengthoccured(int n_words, int wordlen1[]);
int main()
{
char *line = new char[256];
cout<<" Enter any line : ";
cin.getline(line, 256);
char **words = getWords(line);
int num_words = normalPrint(words);
reversePrint(words, num_words - 1);
backwardsPrint(words);
cout<<" Number of words : "<<num_words;
lettercount(words);
return 0;
}
char ** getWords(char *line)
{
char *words[256];
int i=0;
int word_count = 0, word_index=0;
for(i = 0; i < 256; ++i)
{
words[i] = 0;
}
i=0;
words[word_count] = new char[256];
while(line[i] != '')
{
if(line[i] == ' ')
{
words[word_count][word_index] = '';
word_count = word_count + 1;
words[word_count] = new char[64];
word_index = 0;
}
else
{
words[word_count][word_index] = line[i];
word_index = word_index + 1;
}
++i;
}
return words;
}
int normalPrint(char **words)
{
int index = 0;
cout << " Normal Print: " << endl;
while(words[index] != 0)
{
cout << words[index] << " ";
++index;
}
cout << endl;
return index;
}
void reversePrint(char **words, int index)
{
cout << " Reverse Order: " << endl;
while(index >= 0)
{
cout << words[index] << " ";
--index;
}
cout << endl;
}
int getLength(char *word)
{
int index = 0;
while(word[index] != '')
{
++index;
}
return index;
}
void backwardsPrint(char ** words)
{
int index = 0;
cout << " Backwords words: " << endl;
while(words[index] != 0)
{
for(int i = getLength(words[index])-1; i>=0; --i)
{
cout << words[index][i];
}
cout << endl;
++index;
}
}
void lettercount(char ** words)
{
int index = 0;
int wordlen[100];
cout << " letters in each word: " << endl;
while(words[index] != 0)
{
cout << words[index]<<" - "<<getLength(words[index]);
wordlen[index]=getLength(words[index]);
cout << endl;
++index;
}
smallestword(index-1,wordlen);
longestword(index-1,wordlen);
lengthoccured(index-1,wordlen);
}
void smallestword(int n_words,int wordlen1[])
{
int smallwordlen=wordlen1[0];
for(int i=0;i<n_words;i++)
{
if(wordlen1[i]<smallwordlen)
{
smallwordlen=wordlen1[i];
}
}
cout<<" Smallest word`s Length : "<<smallwordlen;
}
void longestword(int n_words,int wordlen1[])
{
int longwordlen=wordlen1[0];
for(int i=0;i<=n_words;i++)
{
if(wordlen1[i]>longwordlen)
{
longwordlen=wordlen1[i];
}
}
cout<<" Longest word`s Length : "<<longwordlen;
}
void lengthoccured(int n_words, int wordlen1[])
{
for(int i=0;i<=n_words;i++)
{
int count=0;
for(int j=0;j<=n_words;j++)
{
if(wordlen1[i]==wordlen1[j])
{
count++;
}
}
cout<<" Word of Length "<<wordlen1[i]<<" occured : "<<count<<" times";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.