Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 10-01-2009
Member
 
Join Date: Jan 2009
Posts: 8
Length of a string displayable in C

Does anyone know if there is a function in lib or libX11 which returns the number of characters? (strlen () is not appropriate because it returns the number of bytes)
Eg "/hame/big/proj$", strlen () returns me 19 while only 15 characters 'to', '?', 'i', 'O' which are 2octets characters.

I made a function:
Code:
unsigned int StringLength(char *str)
  ( 
    unsigned int nb; 
    char c, flg; 

    for (nb = 0, flg = 0; (c = *str) != '\0'; ++str, ++pos) { 
      if (isprint(c) != 0){ // classical character display                                                                                      
        flg = 0; 
        ++nb; 
      ) 
      else if (flg == 0) // if it was the first byte of a 2octets character  
        ++flg; 
      else if (flg == 1) { // 2nd characters display the 2octets 
        ++nb; 
        flg = 0; 
      ) 
    ) 
    if (flg == 1) // If the last character was the 2nd byte character 2octets 
      ++nb; 
    return (nb); 
  )
Reply With Quote
  #2  
Old 10-01-2009
Member
 
Join Date: May 2008
Posts: 2,293
Re: Length of a string displayable in C

it's utf8:
Code:
size_t strlen_utf8 (const char * const s) 
  ( 
     size_t i = 0; 
     size_t j = 0; 
   
     while (s[i])  
     ( 
        if ((s[i] & 0xc0) != 0x80)  
           j++;
         
        i++; 
     ) 
   
     return j; 
  )
Reply With Quote
  #3  
Old 10-01-2009
Member
 
Join Date: Apr 2008
Posts: 2,001
Re: Length of a string displayable in C

If your string is a wide char (wchar_t-based), the function is wcslen
If your string is encoded in utf8 mblen should do the trick.
Reply With Quote
  #4  
Old 10-01-2009
Member
 
Join Date: Jan 2009
Posts: 8
Re: Length of a string displayable in C

What a solution... Wow. Thank you.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Length of a string displayable in C"
Thread Thread Starter Forum Replies Last Post
Finding php string length afidelino Software Development 5 26-02-2010 07:34 PM
How to reduce URL length Mahish Technology & Internet 4 05-02-2010 05:58 AM
Get String Length with Javascript klite Software Development 3 31-10-2009 11:39 AM
How to Manipulate String using PHP String Functions ComPaCt Software Development 3 21-09-2009 09:07 AM
max length filename John A Grandy Windows Server Help 6 23-10-2008 11:00 PM


All times are GMT +5.5. The time now is 01:01 AM.