Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Find and count spaces, letters and words using pointers

obtuseobtuse Posts: 2Member
I'm fairly new to pointers so I'm having some problem with this. I'm trying to write a program to receive a sentence as input, use pointers to find the spaces in the sentence, count the words (which would be numbers of spaces + 1) and find the number of letters. The compiler tells me:
[code]c: emplab11outlab11out.cpp(37) : error C2664: 'getWords' : cannot convert parameter 2 from 'char *' to 'char *[]'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
c: emplab11outlab11out.cpp(40) : error C2664: 'displayInfo' : cannot convert parameter 2 from 'char *' to 'char *[]'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
c: emplab11outlab11out.cpp(58) : error C2440: '=' : cannot convert from 'char' to 'char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
c: emplab11outlab11out.cpp(64) : error C2440: '=' : cannot convert from 'char' to 'char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast [/code]

This is the code:
[code]

#include
#include

using namespace std;

void getString(char* string, int max_length);
void getWords(char *orig_string, char* words[], int* word_length, int& wc);
void displayInfo(char* orig_string, char* words[], int* word_length, int word_count);

int main()
{
//Variable Declaration/Initialization
const int MAX_LENGTH = 255;
char name[MAX_LENGTH] = {''};
char *string;
const int MAX_ELE = 50;
char ptr[MAX_ELE];
char* words;
int wl[MAX_ELE];
int* word_length;
int wc = 0;

//Make string and words point to set array
string = name;
words = ptr;
word_length = wl;

//Function call

//1. Prompting user for sentence
getString(name, MAX_LENGTH);

//2. Counting words in sentence and letters in words
getWords(name, words, word_length, wc);

//3. Displaying info
displayInfo(name, words, word_length, wc);

return 0;
}

void getString(char* string, int max_length)
{
cout << "Original string: ";
cin.getline(string, max_length);
}

void getWords(char* orig_string, char* words[], int* word_length, int& wc)
{
//Allocating adress of first letter of each word
int i = 0;
int wl = 0;
int count = 0;
//Trying to assign the adress of the first letter to the first element of pointer words array
words[i] = orig_string[i];
for( i = 0; orig_string[i] != ''; i++ )
{
if( orig_string[i] == ' ' )
{
count++;
words[i] = orig_string[i];
}
}


//Word length
int j = 0;
while (j < i)
{
//Count letters as long as null terminator is not reached
while (words[j] != '')
{
wl++;
}
//Increase counter for each word
j++;
}

word_length[j] = wl;

//Word count
wc = i+1;
}

void displayInfo(char* orig_string, char* words[], int* word_length, int word_count)
{
cout << "Number of words: " << word_count << endl;
for (int i = 0; i < word_count ; i++)
{
cout << left << setw(20) << "Word 1: " << *words[i];
cout << left << setw(20) << "Length of Word " << i+1 << ": " << word_length[i];
}
}[/code]
Sign In or Register to comment.