Thread: Strlen

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    23

    Strlen

    How do I find the number of alphanumeric in a string?

  2. #2
    Registered User planet_abhi's Avatar
    Join Date
    Oct 2002
    Posts
    92
    i think u r very newbie to prog


    Use strlen function
    Syntax: strlen(string);
    AbHHinaay

  3. #3
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    Re: Strlen

    Originally posted by NavyBlue
    How do I find the number of alphanumeric in a string?
    This is how you use strlen

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
            char name[] = "Datainjector";
    
            printf( "Your string is %lu long\", strlen(name));
            return 0;
    }
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>alphanumeric
    ... would suggest you can't use strlen(), as this will include all characters up the \0. Maybe you don't want non-alpha numerics counted.

    Maybe you need to write your own version, and use something like isalnum().
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    79
    Try this.
    Code:
    #include<ctype.h>
    #include<stdio.h>
    #include<string.h>
    #include<conio.h>
    int main()
    {
       int ctr,ctr2;   
       char string[]="Sample String";
       ctr2=0;
       for(ctr=0; ctr<strlen(string); ++ctr)
       {
          if(isalnum(string[ctr]))
             ++ctr2;
       }
       printf("The number of alphanumeric characters is %d.",ctr2);
       getch();
       return(0);
    }
    Compiler : Turbo C++ v1.01.
    Last edited by sundeeptuteja; 11-10-2002 at 05:54 AM.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>char string[]="Sample String";
    string is a reserved name, therefore you shouldn't use it.

    >>for(ctr=0; ctr<strlen(string); ++ctr)
    Checking the length of the string everytime you go round the loop isn't helping efficiency. It'd be better to store the length in a variable and use that to control the loop.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Jun 2002
    Posts
    79
    The program seems to work fine on Turbo C++ v1.01, as a C source file. Are you sure string is a reserved name? I am relatively new to C, just a first year college student.
    Last edited by sundeeptuteja; 11-11-2002 at 05:35 AM.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>The program seems to work fine on Turbo C++ v1.01, as a C source file.
    That maybe so, but it doesn't make it correct as per the C Standard.

    >>Are you sure string is a reserved name?
    Yes, or I would not have said so.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

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, 03: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, 10: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