Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding Space counter to program
    primarykey
    data
    text
    <p>I have a program that replaces spaces with dash. Now I need to be able to count the amount of spaces that have been replaced and print it. here is my coding for the spacing replace.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;conio.h&gt; #include &lt;string.h&gt; int main() { char string[100], *space; { printf("Enter a string here: \n"); //Enter a string in command prompt fgets(string, sizeof(string), stdin); //scans it and places it into a string space = string; while (*space == ' '? (*space = '-'): *space++); printf("%s\n", string); } getchar(); } </code></pre> <p>and this is the code to count the number of space.</p> <pre><code>#include &lt;iostream&gt; #include &lt;string&gt; int count( const std::string&amp; input ) { int iSpaces = 0; for (int i = 0; i &lt; input.size(); ++i) if (input[i] == ' ') ++iSpaces; return iSpaces; } int main() { std::string input; std::cout &lt;&lt; "Enter text: "; std::getline( std::cin, input ); int numSpaces = count( input ); std::cout &lt;&lt; "Number of spaces: " &lt;&lt; numSpaces &lt;&lt; std::endl; std::cin.ignore(); return 0; } </code></pre> <p>I'm not sure how to put the 2 combined together? Can anyone help?</p> <p><strong>UPDATE:</strong></p> <p>I have changed my code to the following:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;conio.h&gt; #include &lt;string.h&gt; int numSpaces = 0; int main() { char string[100], *space; { printf("Enter a string here: \n"); //Enter a string in command prompt fgets(string, sizeof(string), stdin); //scans it and places it into a string space = string; while (*space == ' '? (*space = '-'): *space++); printf("%s\n", string); } while (*space) { if( *space == ' ') { *space = '-'; ++numSpaces; } ++space; printf("%f\n", numSpaces); } getchar(); } </code></pre> <p>Problem with the result. I keep getting loads of Zeros</p> <p><img src="https://i.stack.imgur.com/OtkBv.png" alt="enter image description here"></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