String Length function

This is a discussion on String Length function within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, I have been learning about strings and successfully wrote my first string program which counts the characters in ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    48

    String Length function

    Hi everyone, I have been learning about strings and successfully wrote my first string program which counts the characters in a given sentence. The problem that I ran into was that I wanted the program to count a string of characters that was entered by the user (no matter what it was)
    e.g.
    My name is Glenn
    output: 16 (including spaces)

    This is the program I was able to successfully run
    Code:
    #include<iostream.h>
    #include<cstring>
    
    int main()
    {
    char *string_a= "This is a test";
    
    cout<<"The length of \""<<string_a<<"\"is "<<strlen (string_a)<<endl;
    
    return 0;
    }
    This is the program that I am trying to write so that it returns the number of characters input by the user
    Code:
    #include<iostream.h>
    #include<cstring>
    
    int main()
    {
    char *string;
    
    cout<<"What is your string";
    cin>>string;
    
    cout<<" The length of "<<string<<" is " << strlen (string)<<endl;
    
    return 0;
    }
    I thought that the program was written correctly because it returns no (compiling) error but when it compiles Visual C returns an error and closes my program out.


    Can someone please poing me in the right direction....Should I start over??

    Thank you very much

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    char *string;
    cout<<"What is your string";
    cin>>string;

    You need to allocate space for your pointer
    Easy suggestion is to start with
    char string[100];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    If you're including the string library, why not just use string variables instead of char arrays?
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    #include<cstring> - For c style string manipulation like strlen, strcat, strcmp - the new version of <string.h>.
    #include<string.h> - Deprecated version of <cstring>.
    #include<string> - For the standard string class (std::string).

    Even though you aren't actually including the string library, I would definitely agree with joshdick to use the standard string class. Also, why do you use the old <iostream.h> but the new <cstring>? Just stick to the new headers (e.g. <iostream>).

    And finally, if for some reason you do need to use c-style strings, I would add to Salem's advice by protecting the input so that it won't overflow using setw.

    [Edit: See later post for solution that better solves the OP's problem (this one ignores the inclusion of space characters in the string).]

    So here is my preferred solution:
    Code:
    #include<iostream>
    #include<string>
    
    int main()
    {
        std::string string_a;
    
        std::cout<<"What is your string: ";
        std::cin>>string_a;
    
        std::cout<<" The length of "<<string_a<<" is " << string_a.length()<<std::endl;
    
        return 0;
    }
    And here is a solution with c style strings:
    Code:
    #include<iostream>
    #include<cstring>
    #include<iomanip>
    
    int main()
    {
        char string_a&#091;100&#093;;
    
        std::cout<<"What is your string: ";
        std::cin>>std::setw(100)>>string_a;
    
        std::cout<<" The length of "<<string_a<<" is " << strlen (string_a)<<std::endl;
    
        return 0;
    }
    Last edited by jlou; 10-07-2003 at 06:13 PM.

  5. #5
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Thanks for clearing that up for me, jlou.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Sorry, I didn't notice that you really want the input with the spaces. Here are the two pieces of code above corrected to accept the entire line.

    Preferred using standard string:
    Code:
    #include<iostream>
    #include<string>
    
    int main()
    {
        std::string string_a;
    
        std::cout<<"What is your string: ";
        std::getline(std::cin, string_a);
    
        std::cout<<" The length of \""<<string_a<<"\" is " << string_a.length()<<std::endl;
    
        return 0;
    }
    And using c-style strings:
    Code:
    #include<iostream>
    #include<cstring>
    
    int main()
    {
        char string_a&#091;100&#093;;
    
        std::cout<<"What is your string: ";
        std::cin.getline(string_a, 100);
    
        std::cout<<" The length of \""<<string_a<<"\" is " << strlen (string_a)<<std::endl;
    
        return 0;
    }

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    48

    Thanks

    Hi everyone, I just wanted to say thanks for the help. I was able to clean up my code and run it successfully.

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21