Hello everyone,
Ok my problem is that I do not get how to convert an array of characters to a string arrays. Like I do not get how to assign a null character at the end of a set of characters... how do I incorporate punctuation, spaces etc in this concept. Can someone explain this to me in really simple words? Thanks in advance.

~Sharokh

Recommended Answers

All 11 Replies

Find the end of the 'string' -- the last character
Say it's array[x] Set array[x+1] to '\0'
The char array is now a string.

Find the end of the 'string' -- the last character
Say it's array[x] Set array[x+1] to '\0'
The char array is now a string.

Thank you for your response. What if my character arrays is like a sentence For example "Hello World! How are you?" right now the array is something like:

H
e
l
l
o
W
o
r
l
d!
H
o

etc...how do i make it so it combines the words into a string array of:
Hello
World
!
How
are
you?

Think about it... What kind of character array(s) are needed to do this?
Try something... See if you figured it out.

I was thinking of making a for loop

for (int j = 0; i < j; j++)
{
    string[j] = array[i];
    cout << string[j] << endl;
}

How do you tell when you've come to the end of one word and the start of the next when looking at a sequence of characters?


Be consisent. If punctuation is considered part of a word in one setting, then it always is.

World
!
you?

is not consistent when it comes to parsing out words.

How do you tell when you've come to the end of one word and the start of the next when looking at a sequence of characters?


Be consisent. If punctuation is considered part of a word in one setting, then it always is.

World
!
you?

is not consistent when it comes to parsing out words.

you're right, I'm sorry I made a mistake I meant punctuation is not apart of the word. So:

World
you
?

is what i need. And that's the problem I'm having, how do I add the null character???

And that's the problem I'm having, how do I add the null character???

No, what the problem you have is that you refuse to think about help the help given and you won't answer important questions:

Think about it... What kind of character array(s) are needed to do this?

How do you tell when you've come to the end of one word and the start of the next when looking at a sequence of characters?

Neither question was even considered.

And I already showed you how to add a null character.

I therefore suspect your question is not addressing the real problem you are having. But you haven't given us enough details to understand what that problem really is.

you tell that you've come to the end of a word by a space...but what it tgere is punctuation?

How do you tell what's at the end of a "word" when you are reading? You look at the last char of the "word", correct? So, if you want to peel away any punctuation that might be placed imediately after the last alphabetical character in a given word you will have to look at the last char of every word isolated (by finding spaces between the words or whatever).

This is what I have. Honestly I don't get. When I initially import my array of characters it doesn't account for the space in between the words. So then when I simply set str = array it sticks everything together. Also the punctuation, as you can see, I'm simply using "if" loops which I take to be wrong... SOMEONE PLEASE HELP!!!

char GetToken(char array[])
{
    ifstream myfile("Text1.txt");

	int a = 0;

    if(!myfile)
	{
		cout << "Error opening file." << endl;
		return -1;
	}

	char nextToken;

	while (myfile >> nextToken)
	{
        array[a] = nextToken;
        a++;
	}

    for (int i = 0; i < a; i++)
    {
        cout << array[i] << endl;
    }

    string str;

    for (int j = 0; j < a; j++)
    {
        if (array == "." || array == ";" || array == ":" || array == "!" || array == "?")
        {
            str = array;
            cout << str << endl;
        }
    }
}

array as you have declared it is an array of type char, which could be a single string if it is null terminated. Since you are trying to break up an input string into smaller strings and remember the smaller strings so you can use them later, then you want an array of strings, not a single one. An array of C style stings you would be:

char arrOfCstyleStrings[MAXSTRINGS][MAXSIZE];

where MAXSTRINGS is the maximum munber of strings the container will hold, and MAXSIZE is the maximum length of the string (including the null terminating char). Both MAXSTRINGS and MAXSIZE must be of type const int and the value must be known at time of compiling the program.

If you know about STL string objects, then you could use this instead:

string arrayOfSTLStringObjects[MAXSTRINGS];

again where MAXSTRINGS is used a before, or you could use an STL vector object as opposed to an array like this:

vector<string> arrayOfSTLStringObjects;

if you are know about and are allowed to use vectors in addition to STL strings.

Once you have each word (substring) in the sentence (input string) isolated, then you can look at the last char in each word to see if it is a punctuation mark. There is an ispunct() function that you could use or you could use an if statement similar to what you have written, except you should compare char, not strings.

If you find a punctuation mark at the end of the string, then assign it to a new string by itself, store the new string in the array of strings and overwrite the punctuation mark in the string that you found it in by assigning a null char to that element of the string.

To store the punctuation mark string in the array of strings you will need to shift all the string to the right of the word that you found the punctuation mark in to the left by one (start at the end and work backward), and then overwrite the string just to the left of the string you found the punctuation mark in with the punctuation mark string.

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.