Thread: Looking for spaces in a string of chars.

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    15

    Question Looking for spaces in a string of chars.

    I am trying to to get this program to find spaces in a
    string of characters and display the count.
    I am not sure of my codes and the error that I got seems
    a little 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 <iostream>
    #include <cstring>
    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]= '\0'; 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:\Documents\Desktop\findBlanks#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)

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    reply

    for (count = 0; line[count]= '\0'; count++)

    This seem a little strange. You set all elements to NULL. It should be line[count] != '/0' if I understand it right. Or why not do this:
    for(count=0; count<strlen(line); count++)
    (strlen calculates the length of a string)
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    Counting is as simple as calling std::count:
    Code:
    #include <iostream>
    #include <algorithm>
    
    // Returns how many elements an array has.
    template<class T> inline size_t elems(const T& array) {
        return sizeof array / sizeof *array;
    }
    
    int main() {
        char line[] = "Hello world! This sentence contains some blanks.";
    
        // You call cout with a range and a value to count.
        // So std::count(line, line + elems(line), ' ') will count
        // all ' ' in the range [line; line + elems(line))
        std::cout << line << "\nThe above line conains "
            << std::count(line, line + elems(line), ' ') << " blanks.";
    }
    - lmov

  4. #4
    Unregistered
    Guest
    Hello,

    Could you explain

    ----------------------

    // Returns how many elements an array has.
    template<class T> inline size_t elems(const T& array) {
    return sizeof array / sizeof *array;
    }

    ---------------------

    A little more in detail for me? I have been taking classes for about 2 years and I haven't seen this used before, and it looks like a nice piece of code.

    Btw, im not the one who started this thread, just curious.

  5. #5
    Unregistered
    Guest
    Imov has a compiler that is compliant in use of namespaces and the standard template library. Unfortunately, many of us do not. Therefore we can not make use of functions present in STL llike count().

    The good news is that even many of the older compilers do allow templates. Templates allow you to postpone declaring the actual type of a variable to be used in a function or a class until run time. As long as the class has all the necessary functionality for the template to work it will be useable. This allows you more flexibility and reuseability when writing code. To indicate use of templates you need to use the key word template and then enclose the name of the template variable(s) you are going to allow in angled brackets. Then you can write out the code using the template class whereever indicated. In this case the function will calculate the number of elements in an array using the sizeof operator by calculating the size of the whole array and dividing it by the size of the first element in the array. The return type of the function is in size_t which is a "fancy" way of saying type int since not all systems use the same memory pattern for type int, some use 16 bit ints and some 32 bit ints. Inline is another keyword that means the compiler has a variety of options as to how it wishes to deal with this function in order to tweak as much performance from the function as possible. You should never inline large functions as you are likely to defeat the purpose of inlining in the first place. Many people seldom use this syntax, but it is something you should know a little about anyway.

    if you have an older compiler and you don't want to get fancy you could do this:

    for(int i = 0; i < strlen(line); i++)
    {
    if(line[i] == ' ')
    count++;
    }
    }

    In fact that should work anywhere, but it's not as sophisticated as the code in Imov's post.

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    Imov has a compiler that is compliant in use of namespaces and the standard template library. Unfortunately, many of us do not.
    GCC, one of the best compilers I've seen, is available free for download at http://gcc.gnu.org. The Windows version is available at http://www.mingw.org. You can also get a free IDE at http://www.bloodshed.net.
    Last edited by lmov; 01-26-2002 at 12:06 PM.
    - lmov

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM