I tried solving this all day but I cannot find an adequate solution. I want to print all words of an input char array, but if I type in an empty space at the start or at the end of the array my result is wrong. Does somebody know how to fix this or does somebody have an understandable solution for me? Thank you! Using a library would be okay to if it is understandable :)
#include <iostream>
#include <ctype.h>
#include <stdio.h>
#include <cmath>
#include <string>
#include <stdlib.h>
#include <sstream>
using namespace std;
const int len = 1000;
char inputnames[len];
int main()
{
int counter = 0, z = 0;
cout << "Type in the Candidates names and press enter please: ";
cin.getline(inputnames, len);
string stringstream(inputnames);
string token;
char LastCharacter = stringstream.back();
if (LastCharacter == ' ') {
counter = 0;
for (int z = 0; z < len; z++) {
if (inputnames[z] >= 'a' && inputnames[z] <= 'z' && inputnames[z + 1] == ' ' || inputnames[z] >= 'A' && inputnames[z] <= 'Z' && inputnames[z] == ' ')
{
counter++;
}
cout << inputnames[z];
}
}
else if (LastCharacter != ' ') {
counter = 1;
for (int z = 0; z < len; z++) {
if (inputnames[z] >= 'a' && inputnames[z] <= 'z' && inputnames[z + 1] == ' ' || inputnames[z] >= 'A' && inputnames[z] <= 'Z' && inputnames[z] == ' ')
{
counter++;
}
cout << inputnames[z];
}
}
[EDIT] Hello guys, I solved the last bug now, which involved +1 count if user typed in a space bar at the end of the input message. Please let me know if you have further tips/help/critic. Thank you all!