Find and count spaces in a string of chars.. - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Find and count spaces in a string of chars..

big_edbig_ed Posts: 14Member
I am trying to to get this program to find and count the spaces in a string of characters and display the count.
I am not sure of my codes and the error message that I got seems a little bit strange. Can anyone look at my codes and give me some pointers.
Thank you
My os is VC++ ver. 6.0, I have a desktop using win 2k.
=================
My codes and the returned errors.
+++++++++++++++++++++++++++
#include
#include
using namespace std;

// not used --->char locate_blank(char *);

char line[] = "This is a string test. It contains two sentence.";

int main()
{
int count;
cout << "The original sentence is : " << line;

for (count = 0; line[count]= ''; count++)
{
if (line[count] == ' ')
{
line[count+1] = ' ';
break;
}
return count;
}
cout << "This sentence contains" << count(line) << "blanks";
}

/* Error list
------Configuration: findBlanks#2 - Win32 Debug-------
Compiling...
findBlanks#2.cpp
C:DocumentsDesktopfindBlanks#2.cpp(23) : error C2064: term does not evaluate to a function
Error executing cl.exe.

findBlanks#2.obj - 1 error(s), 0 warning(s)
-=-=-=-=-=-=-=-=-=--=

Comments

  • LopezLopez Posts: 75Member
    : #include
    : #include
    : using namespace std;
    :
    : // not used --->char locate_blank(char *);
    :
    : char line[] = "This is a string test. It contains two sentence.";
    :
    : int main()
    : {
    : int count;
    : cout << "The original sentence is : " << line;
    :
    : for (count = 0; line[count]= ''; count++)
    : {
    : if (line[count] == ' ')
    : {
    : line[count+1] = ' ';
    : break;
    : }
    : return count;
    : }
    : cout << "This sentence contains" << count(line) << "blanks";
    : }


    A few things,

    the line [code] for (count = 0; line[count]= ''; count++) [/code] you have put line[count]= '' instead of a double ==, sould be line[count]== ''

    You also need another variable to actually count the amount of spaces in the text. You are trying to re-use count, which is what you are using as a pointer to count through your text. Also you've put a single = again.

    The error message you're getting, is probably because you also have the code [code]cout << "This sentence contains" << count(line) << "blanks";
    }[/code] outside of function main()

    I don't think you need break; in an if statement. This may be causing trouble, as it could be breaking out of the for loop as soon as it find one space.

    Hope this helps.
  • FelixFelix Posts: 438Member
    : the line [code] for (count = 0; line[count]= ''; count++) [/code] you have put line[count]= '' instead of a double ==, sould be line[count]== ''
    [code]
    line[count]!= ''
    [/code]

    ---
    [b]edx[/b] - Member of [blue][b]CodeForce[/b][/blue] (http://codeforce.d2g.com)

  • OptiKronOptiKron Posts: 21Member
    whoou what a mess ;)
    Let's check everything :)
    : char line[] = "This is a string test. It contains two sentence.";
    :
    : int main()
    : {
    : int count;
    : cout << "The original sentence is : " << line;
    :
    : for (count = 0; line[count]= ''; count++)
    The other guys noticed the fault but they were wrong about one thing : it isn't '==' that you should put but '!=', othewise, the loop will go nowhere and will even crash if line was empty :) so the correct line is :
    for (count = 0; line[count] != ''; count++)

    : {
    : if (line[count] == ' ')
    : {
    : line[count+1] = ' ';
    errr now i don't follow you. I don't understand what you are doing here. you want to count the number of blank space right? So why do you CHANGE your string? Why do you add a blank space just after you found one???? Be sure it will crash, you'll get out of the array if there is only one ' ' in the string :)
    : break;
    : }
    : return count;
    : }
    : cout << "This sentence contains" << count(line) << "blanks";
    Now that's what the error the compiler gives you. The problematic thing is 'count(line)'. It doesn't mean anything. count is a variable, not a function.
    : }

    Here is how i would solve your problem :

    int main()
    {
    int i,count;
    count = 0;
    for( i = 0; line[i] != ''; i++ )
    {
    if( line[i] == ' ' )
    count++;
    }
    cout << "This sentence contains" << count << "blanks";
    }

    You see, we go through the string thx to the var 'i' then, everytime we find a blank space, we just increase the var 'count'.

    Hope it helps :)
Sign In or Register to comment.