Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For starters, you should use the standard functions <code>isupper</code>, etc. (in <code>&lt;ctype.h&gt;</code>&mdash;just remember that you must convert any <code>char</code> to <code>unsigned char</code> before using them). In particular, your code seems to assume that 1) the encoding is ASCII (although no modern machine uses ASCII), and 2) that the word only contains alphabetic characters. (You also fail to check if the input succeeded.)</p> <p>Second, the specified condition involves the entire string, not just the first character. Just testing the first character cannot possibly suffice; you must check every character in the string. (The actual specification of the problem is not too clear; what does "all letters except the first" mean if there is only a single letter?) </p> <p>Third, you loop on the tranformation regardless of the results of your <code>if</code>. Which is certainly wrong.</p> <p>Fourth, you don't swap case; you only mangle values less than 97.</p> <p>There are two separate problems here. The first is to determine whether the caps lock key has been set. The simplest way is something like:</p> <pre><code>if ( std::find_if( word.begin() + 1, word.end(), []( unsigned char ch ) { return !isupper( ch ); } ) == word.end() ) </code></pre> <p>(This is a simplification, since it returns true for all one character strings. But this is one possible interpretation of the specifications, although I don't think a very intuitive one.)</p> <p>If you don't want to use functions like <code>std::find_if</code>, you should factor this out into a separate function:</p> <pre><code>bool capsLockSet( std::string const&amp; word ) { auto current = word.begin() + 1; while ( current != word.end() &amp;&amp; isupper( static_cast&lt;unsigned char&gt;( *current ) ) ) { ++ current; } return current == word.end(); } </code></pre> <p>Similarly, you can use <code>std::transform</code> for the transformation (only done if the above returns true), or factor the loop out into a separate function; in both cases, the first character will require special handling, because if it is lower case, you have to convert it to upper. (<em>IF</em> you have a precondition that the only characters you will see are ASCII letters, then you can toggle between upper and lower by <code>ch ^= 0x20</code>. I'd recommend against it, since it is so dependent on the encoding, <em>and</em> the fact that there are only letters present, and no other characters.)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload