how to get the length of the string in one line

This is a discussion on how to get the length of the string in one line within the C Programming forums, part of the General Programming Boards category; i get a string str in a signature and i dont have an index=0 to use i cant use strlen ...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    how to get the length of the string in one line

    i get a string str in a signature
    and i dont have an index=0 to use

    i cant use strlen because its a function it will give 1 even if the function has 100 cells
    i need to do it in one line
    len=??1??

  2. #2
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Please show what related code you have already, and please please please try to clarify the issue you are having. Your fragmented question provides literally no context to understand your problem.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Code:
    what(char *str)
    {
    int len;
    len=??
    }

  4. #4
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Ok so why doesn't this work?

    Code:
    len = strlen(str);
    http://www.cplusplus.com/reference/c...ng/strlen.html
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    129
    If strlen gives 1 for a char[100], it's because the second character is the null terminator ('\0'). strlen tells the number of characters which precede the null terminator, not how many bytes are allocated to a variable.

    For example:
    Code:
    int main(int agrc, char **argv){
        char *foo = "ab\0def";
        printf("The string \"%s\" is %i characters long.", foo, strlen(foo));
    }
    That will print:
    The string "ab" is 2 characters long.

  6. #6
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    because its in external function and we have our array decays to one cell

  7. #7
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    What?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  8. #8
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Sounds like you're trying to implement something to mimic decay mechanism/function of some sort. Might help if you print the block of code that's implementing your logic as I suspect that's where you're going wrong.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  9. #9
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Code:
    what(char *str)
    {
      int len;
          len = sizeof(str)/sizeof('a'); // Won't work!
    }
    how to write so it will work
    ??
    Last edited by transgalactic2; 03-24-2009 at 01:41 AM.

  10. #10
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i tried
    Code:
    #include <stdio.h>
    #include <string.h>
    void what(char* str);
    int main()
    {
    	char arr[5]={'a','b','c','d','e'};
         what(arr);
    	return 0;
    }
    
    void what(char* str)
    {
    int len;
    len=strlen(str);
    printf("%d",len);
    }
    it doesnt work

  11. #11
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by transgalactic2 View Post
    it doesnt work
    Of course not. You don't have a Null terminator at the end of your string. So strlen will go off searching through memory until it finds one.
    if you wrote:
    Code:
    char arr[6]={'a','b','c','d','e', '\0'};
    That should work.

    Quantumpete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  12. #12
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Code:
    #include <stdio.h>
    #include <string.h>
    void what(char* str);
    int main()
    {
    	char arr[6]={'a','b','c','d','e','\0'};
         what(arr);
    	return 0;
    }
    
    void what(char* str)
    {
    int len;
    len=strlen(str);
    printf("%d",len);
    }
    i changed it it works but i get this warning
    c(14) : warning C4267: '=' : conversion from 'size_t' to 'int'
    what is size_t why i get this warning

  13. #13
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    when I run your code, the version with:
    Code:
    char arr[5]={'a','b','c','d','e'};
    I get the output 7
    but with the null terminated one I get the correct output.
    What compiler are you using?
    I used dev C++.
    Last edited by WDT; 03-24-2009 at 03:13 AM.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  14. #14
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Quote Originally Posted by transgalactic2 View Post
    Code:
    #include <stdio.h>
    #include <string.h>
    void what(char* str);
    int main()
    {
    	char arr[6]={'a','b','c','d','e','\0'};
         what(arr);
    	return 0;
    }
    
    void what(char* str)
    {
    int len;
    len=strlen(str);
    printf("%d",len);
    }
    i changed it it works but i get this warning
    c(14) : warning C4267: '=' : conversion from 'size_t' to 'int'
    what is size_t why i get this warning
    ????

  15. #15
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Stop reposting your questions! It's annoying.
    As to size_t, it's a datatype. In this case strlen returns size_t (which is probably an unsigned int or something). You declared len as an (signed) int.
    to make the program correct, you should either use unsigned int (since strlen cannot return a -ve number) or use size_t len;

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Page 1 of 2 12 LastLast
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, 07:38 PM
  2. help again with scrolling without wrapping
    By Dukefrukem in forum C Programming
    Replies: 8
    Last Post: 09-21-2007, 12:48 PM
  3. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  4. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  5. Weird modification to string length
    By ChwanRen in forum C Programming
    Replies: 0
    Last Post: 08-17-2003, 10:45 AM

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