Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to initialize <code>charcount</code>. Other than that, it should work, provided that <code>z</code> is a zero-terminated array of characters and <code>m</code> is an <code>int</code> or similar. I would probably write just <code>z[m]</code> rather than <code>z[m] != 0</code> (since !0 = true and 0 = false), but both work. There are more efficient ways of doing it (although these days I bet a compiler will handle converting this into a pointer-based loop for you).</p> <p>Here's a complete, correct example with minimal edits:</p> <pre><code>const char * z = "testing one two three"; int m; int charcount; charcount = 0; for(m=0; z[m]; m++) { if(z[m] != ' ') { charcount ++; } } </code></pre> <p>If you're using a <code>String</code> class of some kind rather than an old-fashioned C null-terminated array, you'll want to look at that class for how to loop through it.</p> <p>All of the above also assumes you're dealing with ASCII strings. If you're dealing with UTF-encoded strings, you have to handle multi-byte characters.</p> <hr> <p>Re your edit: It makes a big difference: <code>scanf</code> will <a href="http://www.cplusplus.com/reference/clibrary/cstdio/scanf/" rel="nofollow noreferrer">stop on the first blank</a> (I'd forgotten that). It might make an even bigger difference than that, though, if you're not declaring <code>z</code> correctly. (I'd also recommend using a field width when using <code>scanf</code> for reading strings [or avoiding <code>scanf</code> entirely]; otherwise, you have no control over the number of chars it will try to store, and so <em>in theory</em>, no buffer will ever be big enough to avoid an overflow. More here: <a href="http://www.crasseux.com/books/ctutorial/String-overflows-with-scanf.html" rel="nofollow noreferrer">http://www.crasseux.com/books/ctutorial/String-overflows-with-scanf.html</a>)</p>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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