strlen

This is a discussion on strlen within the C++ Programming forums, part of the General Programming Boards category; hi everyone, question strlen gives me the length of a string exemple strlen tom will be three, but how do ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    14

    strlen

    hi everyone, question strlen gives me the length of a string exemple strlen tom will be three, but how do i output the character size of the array str[12]

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    do you mean, how do you check how many characters are in str? You'd know that from the declaration, it's a part of the datatype. Alternatively, you can use the sizeof operator, IE

    sizeof( str );

    If you are working with a pointer to the first element and want to check the length of the array itself, for instance, if you dynamically allocated the array, then you can't check the array length -- you'd have to manually keep track of it when you allocate it.

  3. #3
    btq
    btq is offline
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161
    strlen takes a const char* as argument and counts the characters until it hits a null-terminator.
    If I don't got you wrong now, maybe you want to do somethilng like this ? :

    Code:
    char bla[12]="hej";
    int z = strlen((char*)bla);
    z would now be 3...

    hope this helps
    /btq
    ...viewlexx - julie lexx

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by btq
    Code:
    char bla[12]="hej";
    int z = strlen((char*)bla);
    The typecast is unecissary as the value of bla in an expression IS a pointer to a char

    strlen( bla );

    would work exactly the same way.

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    14

    Smile

    thanks a lot Polymorphic OOP!

    that was esactly what i was looking for, it worked just fine,thanks again.

  6. #6
    btq
    btq is offline
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161
    The typecast is unecissary as the value of bla in an expression IS a pointer to a char

    strlen( bla );

    would work exactly the same way.
    yeah, hum...I thought that was strange and unneeded. I got a compiler error before when doing that so I threw in the (char *) and the error went away...now it has dissapeared though.
    Ahh well..

    /btq
    ...viewlexx - julie lexx

  7. #7
    Registered User
    Join Date
    Dec 2002
    Posts
    14
    hi and thanks for your rsponces ,but im stock again at the nd of my progrand, waht i need to do is concanate first 2 characters of one string to another, it does it fine but i get a whole bunch of other character at the end of it plus the same string again, how do i stop that from happening here is some of my code, i have included <cstring>


    Code:
     
    int main()
    {
    	char string1[10];
    	char string2[15];
    	char string3[15];
    	char string4[8];
    
    cout <<"Enter string1: ";
    cin.getline(string1, 10, '\n');
    cout <<"Enter string2: ";
    cin.getline(string2, 15, '\n');
    
    cout<<endl<<string1<<" is "<<strlen(string1)<<" characters in length"<<endl;
    cout<<string2<<" is "<<strlen(string2)<<" characters in length"<<endl;
    
    cout<<endl<<string1<<" is "<< sizeof(string1) << " characters in size"<<endl;
    cout<<string2<<" is "<< sizeof(string2) << " characters in size"<<endl; 
    
    if (strcmp(string1,string2) !=1 || 0)
    {
    cout<<endl<<string1<<" is less than  "<<string2<<endl;
    	
    }		
    	
    
    cout<<endl<< "string3 after concatenation is "<<strcat(string1,string2)<<endl;
    
    
    cout<<endl<<"String 4 is "  <<strncpy(string2,string1,3)<<endl;
    
    
    cout<<endl<<"String 1 is now " <<strncpy(string2, string1,7)<<endl;
    
    return 0;
    }
    im entering billy for string 1
    bob for string 2

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Playing around strlen :)
    By audinue in forum C Programming
    Replies: 6
    Last Post: 06-13-2008, 04:22 PM
  2. strlen help
    By stewie1986 in forum C Programming
    Replies: 10
    Last Post: 12-04-2007, 12:15 PM
  3. strlen in expressions
    By justforthis1 in forum C++ Programming
    Replies: 4
    Last Post: 10-24-2006, 11:28 AM
  4. strlen()
    By exoeight in forum C Programming
    Replies: 9
    Last Post: 04-01-2005, 10:18 AM
  5. Just say NO to strlen.
    By anonytmouse in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 02-11-2005, 01:34 PM

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