Counting how many chars in a string

This is a discussion on Counting how many chars in a string within the C Programming forums, part of the General Programming Boards category; Could someone give me a little nugget of code to count how many charactors there are in a string? Here's ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    32

    Unhappy Counting how many chars in a string

    Could someone give me a little nugget of code to count how many charactors there are in a string?

    Here's a function I was trying to get to count how many chars were in a string:

    Code:
    int current_char;
    static char somestring[5] = { "Hello" };
    
    
    int getstrlength(somestring)
    {
    	for (current_char = 0;current_char < 80;current_char++) {
    		if (somestring[current_char] == '\0') {
    			return (current_char);
    			break;
    		}
    	}
    }
    My compiler, Dev-C++ 5 beta, gives the error: subscripted value is neither array nor pointer (and it doesn't compile). I put the line it reported this on in bold.

    On a sidenote the ASCII tables on www.asciitable.com are not really correct, at least for my PC... some of the box drawing chars at the bottom are really accented A charactors, which turned out to be quite annoying for what I was doing. I'm looking for box drawing chars which join double lines to single lines on corners or T shapes etc.

    Thanks for any help!
    - Tigs

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    32

    Lightbulb D'oh

    Ah, I've been dumb again.

    Code:
    #include <string.h>
    #include <stdio.h>
    
    ...
    printf("%d",strlen("Hello"));
    Problem solved.

    Anyway, any ideas on the missing box drawing chars?

    Thanks.
    - Tigs

  3. #3
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Well I tried this in CodeWarrior. It failed in a QuickWin, but as a console app it worked as expected.

    Code:
    #include <stdio.h> 
    
    int main(void) 
    { 
    	printf("\xC9\xCD\xCD\xBB\n");
    	printf("\xCC\xCD\xCD\xB9\n");
    	printf("\xC8\xCD\xCD\xBC\n");
        
        return 0; 
    }
    An unsigned char should also work if you want a variable.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    32

    Alternative

    I find a good way of seeing an ASCII char from its value is

    printf("%c",200);

    where 200 is the ASCII value.

    I tried some hex values and still couldn't find any double-line-to-single-line box chars. Thanks for your help though.
    - Tigs

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    9
    Code:
    int mystrlen(const char *s);
    
    int main() {
        char str[] = "123456789abcdefghijklmnopqrstuvwxyz";
    
        printf("Length: %d", mystrlen(str));
     
        return 0;
    }
    
    int mystrlen(const char *s) {
        int i;  
    
        for(i = 0;*s;s++)
          i++;   
    
        return i;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Changing chars in a string
    By Hawkin in forum C Programming
    Replies: 6
    Last Post: 02-23-2008, 07:06 PM
  2. traverse a string to look for certain chars
    By cdkiller in forum C++ Programming
    Replies: 7
    Last Post: 09-28-2006, 02:19 PM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  5. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM

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