I'm fairly new to pointers and are having problems with the char* words[] part of this program. It's all fairly simple

#include <iostream>
#include <iomanip>

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] = {'\0'};
	char *string;
	const int MAX_ELE = 50;
	char *ptr[MAX_ELE]; //an array of character pointers previously declared in main with maximum number of elements set to 50
	char* words[MAX_ELE] = {'\0'};
	int wl[MAX_ELE];
	int* word_length;
	int wc = 0;

	//Make string and words point to set array
	string = name;
	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[0] = &orig_string[0];
	for( i = 0; orig_string[i] != NULL; i++ )
	{
		//Check for space
		if( orig_string[i] == ' ' )
		{
			words[count] = &orig_string[i+1];
			count++;
					
		}
	}
	
	
	//Word length
	int j = 0;
	for (int j = 0; j < count; j++)
	{
		//Count letters as long as null terminator is not reached
		if (words[j] != NULL)
		{
			wl++;
		}
		//Assign word length to each element in word_length array
		word_length[j] = wl;
	}
	
	//Word count
	wc = count+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 : " << *words[i];
		cout << left << setw(20) << "Length of Word " << i+1 << ": " << word_length[i];
	}
}
//line #24 you might have to do this
string = &name;

Edited 7 Years Ago by Clinton Portis: girlfriend's age? shfifty-five.

I will assume your problem is here. You test for a space just fine.. but what about the rest of the time when there is stuff to be copied..?

//Try this:

for( i = 0; orig_string[i] != NULL; i++ )
{
     //Check for space
     if( orig_string[i] == ' ' )
     {          
          count++; 
     }
     //Else, add characters to your word
     else
     {
          strcat(words[count] , orig_string[i]);
     }
}

There is probably a better way to do this using strtok().

Edited 7 Years Ago by Clinton Portis: necessito mas cerveza.

This article has been dead for over six months. Start a new discussion instead.