-1

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!

Clemens
  • 11
  • 1
  • 4
  • where are `inputnames` and `len` declared / initialized? – Borgleader Aug 21 '22 at 15:05
  • [tour], [ask], and [example]. It may seem odious to you to have to read this before posting, but it helps you get help faster (even considering the time it takes to read and craft a better question). – JohnFilleau Aug 21 '22 at 15:06
  • "I want to print all words" - Then start by defining "what is a word". That question can have a very surprising list of answers. – Jesper Juhl Aug 21 '22 at 15:07
  • 1
    Before posting their first question on stackoverflow.com, everyone should take the [tour], read the [help], understand all the requirements for a [mre] and [ask] questions here. Not doing any of this results in a poor quality question almost every time. It then gets downvoted, closed, and then deleted. Repeated low-quality questions may result in a temporary ban from asking new questions. – Sam Varshavchik Aug 21 '22 at 15:07
  • Does this answer your question? [How do I iterate over the words of a string?](https://stackoverflow.com/questions/236129/how-do-i-iterate-over-the-words-of-a-string) – JohnFilleau Aug 21 '22 at 15:09
  • Your question specifies char array, but you're open to libraries. Therefore, I suggest using stringstream and string. There are likely more compute-efficient ways to handle this, but consider developer-time as a finite resource as well. Sometimes you push less efficient code because your time is worth more than a few flops. – JohnFilleau Aug 21 '22 at 15:11
  • 1
    This isn't real code that you tried, is it? It doesn't match either your goal or your symptom description. It only outputs the spaces no matter what is entered. – Avi Berger Aug 21 '22 at 15:11
  • Thank you guys. Hope it will be better one day :) – Clemens Aug 21 '22 at 15:39

2 Answers2

1

You're looking for word by checking if there's space followed by any other character. Try checking for letters if(inputnames[z] >= 'a' && inputnames[z] <= 'z') || (inputnames[z] >= 'A' && inputnames[z] <= 'Z') and if the following character is not a letter.

WojciechCode
  • 32
  • 1
  • 6
0

I don't know if I got what is your exact purpose but I can understand the curiosity to solve the issue first you are facing, so please try my changes and let me know if that works for you.

#include <iostream>
#include <ctype.h>
#include <stdio.h>
#include <cmath>
#include <string>
#include <stdlib.h>
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);
    counter = 1;
    for (int z = 0; z < len; z++) {
        if (inputnames[z] != ' ') {
           
           cout << inputnames[z];
        }
        else if(z != 0 && inputnames[z] == ' ' && inputnames[z-1] != ' ' && inputnames[z+1] != ' '){
            cout<<"\n";
            counter++;
        }    
            
    }
    cout << endl;
    cout << counter<< endl;
}
AMS
  • 29
  • 4
  • Dear Mr. AMS, I want to thank you all for the creative input. I want to say sorry for the inconvencience. I am pretty new to coding and everything related. I did not take the time to properly express myself and thus I wrote that I want to print out the strings of the input array, but I meant to say that I want the number of the strings of the input array. The Loop I wrote just does this but I want to allow for a blank space in the start and in the end without the counter going one up. Sorry again.. – Clemens Aug 21 '22 at 15:38
  • @noobi, I have made the changes in code please check. Another suggestion would be to not take len and inputnames in global scope – AMS Aug 21 '22 at 15:46
  • Dear Mr. AMS, thank you that works just fine, exept that I cannot type several blank spaces in the mid of the code :) – Clemens Aug 21 '22 at 15:56