getting string length without using strlen

This is a discussion on getting string length without using strlen within the C++ Programming forums, part of the General Programming Boards category; I have been asked to write a function that performs just as strlen would. I have no idea how to ...

  1. #1
    Unregistered
    Guest

    getting string length without using strlen

    I have been asked to write a function that performs just as strlen would. I have no idea how to do anything on that without using cin.get() or cin.getline. How do I perfom get-like functions on already stored strings

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Count the char's in the string array until you reach null.

  3. #3
    Unregistered
    Guest
    Fordy,
    I know how to do it when entered at keboard but how do you count char on already stored string
    ie

    void main()
    {
    char source_str[128];
    int res;
    cout<<"enter string"<<endl;
    cin>>source_str;
    res=Cstrlen(source_str);
    cout<<res;
    }

    int Cstrlen(const char source_str[])
    {
    count source_str characters here//how?
    return num_char
    }

  4. #4
    Ben K.
    Guest
    You can just do this:

    int main()
    {
    char string[] = {"String"};
    int size=0;

    while(string[size]){
    size++;
    }
    cout << "The lenght is " << size;
    }

    the output should be 6.

  5. #5
    A Banana Yoshi's Avatar
    Join Date
    Oct 2001
    Posts
    859
    this put the length of the String "Name" to an int "x":

    int x = Name.length();
    ---------------------
    Engineer223
    alias: 322reenignE
    Yoshi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange string behavior
    By jcafaro10 in forum C Programming
    Replies: 2
    Last Post: 04-07-2009, 08:38 PM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. Find String length and Arraysearch
    By s.rajaram in forum C Programming
    Replies: 5
    Last Post: 10-03-2007, 03:28 AM
  4. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 10:19 AM
  5. Replies: 4
    Last Post: 03-03-2006, 02:11 AM

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