-3

I'm working on a lab for school. The goal of the last exercise is to make a program that can scan a string input by the user for any words that suffer from "capslock syndrome" (i.e. words that begin with a lowercase letter and have all uppercase letters, lIKE tHIS). This is what I've got so far:

#include<iostream>
#include<string>
#include<cctype>
using namespace std;

int main()
{
string s("");
int len;

do 
{
    cout<<"Enter a string. I will check it for \"caps-lock syndrome\"\n";
    getline(cin, s);
    cout<<endl;
    cout<<s<<"\n\n";

    len = s.length();

    for(int i=0; i<len; i++){
        if(islower(s[i])){
            for(int j=i+1; j<len; j++){
                if(isupper(s[j])){
                    cout<<s[j];
                }
            }
        }
    }

} while(!s.empty());

return 0;
}

What should happen is when the user enters a string like "cAPS lOCK is on" the resulting output should be: cAPS lOCK

And then loop back to the start of the program. The problem I'm getting now is that I can't get the full word to print (just the uppercase characters) and I can't get each word to print to its own line like I want to.

Matt_quad
  • 3
  • 2
  • When input such as `lIKE tHIS` is given what output do you expect? – Shibli Apr 09 '17 at 20:10
  • The intended sequence should be the user enters a string, the program scans for any words that begin with a lowercase letter and have all uppercase letters afterwards, and displays the offending words on the screen. So if I entered a string "cAPS lOCK is on" the output should be: cAPS lOCK (each word on its own line). The problem I'm running into is that I can't figure out how to print the whole word and I can't get them to output on separate lines. – Matt_quad Apr 09 '17 at 20:34
  • Give breaking the line into words with a `std::stringstream` [kind of like option 2 here](http://stackoverflow.com/a/7868998/4581301) and then scanning the words individually a try. That way you only have to look at the first letter of the word as lower. Reduces the forloopage. – user4581301 Apr 09 '17 at 20:57

1 Answers1

0

Here is code that may help you:

string the_string = "hELLO hOW aRE yOU";
vector<string>v;
string new_string = "";
for (int i = 0; i < the_string.length(); i++)
{
   if (the_string[i] != ' ')
   {   
      new_string += the_string[i];
   }
   else
   {
      v.push_back(new_string);
      new_string = "";
    }
}
 //Now, the string vector is loaded

for(int i = 0; i < v.size(); i++)
{
   if (islower(v[i][0]))
   {
     int counter = 0;
     for (int b = 1; b < v[i].length(); b++)
     {
        if (isupper(v[i][b]))
        {
            counter += 1;
        }
     }
     if (counter == v[i].length()-1)
     {
        cout << v[i]<< " has caps lock syndrome"<< endl;

     }
     else
     {
        cout << v[i] << " does not have caps lock syndrome"<< endl;

     }
    }
    else
    {
       cout << v[i]<< " does not have caps lock syndrome"<< endl;
    }
}    
Ajax1234
  • 69,937
  • 8
  • 61
  • 102