How do I find the number of alphanumeric in a string?
How do I find the number of alphanumeric in a string?
i think u r very newbie to prog
Use strlen function
Syntax: strlen(string);
AbHHinaay
This is how you use strlenOriginally posted by NavyBlue
How do I find the number of alphanumeric in a string?
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 ???
>>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]
Try this.
Compiler : Turbo C++ v1.01.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); }
Last edited by sundeeptuteja; 11-10-2002 at 05:54 AM.
>>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]
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.
>>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]