### devsks's blog

By devsks, 3 years ago, ,

In order to find the numbers of characters in an string we can use scansets in C.

The [%n] format identifier stores the numbers of characters read by the input stream i.e stdin to the given address. Eg:

#include<stdio.h>
int main(void)
{
char st[123];

int len;

scanf("%s%n", st, &len); // total no. of characters will be stored at the address of 'len'

printf("%i", len);
return(0);
}


Output: computer 8

We can also use this identifier to count the numbers of digits of a number etc.

•
• +101
•

 » 3 years ago, # | ← Rev. 3 →   -46 Well there are functions like size() and length() if you are using the string variable , dont forget to include the header file #include  or #include  . As far as I know strlen() is not used probably because it takes O(length) time in finding the length of the string but size() function takes only O(log length) time to do it. As you are using char a[100] type of string i think strlen() is the only viable option you have.
•  » » 3 years ago, # ^ |   +74
•  » » 3 years ago, # ^ |   +3 Are you sure that size() takes O(log length) time and not O(1)?
•  » » » 3 years ago, # ^ |   +5 Indeed size() and length() take O(1) time. That was my mistake but i dont know why i aint able to edit or delete my post!! Thats why i didnt change it and the negative feedback was another factor for not changing it!! :(
•  » » » 3 years ago, # ^ |   +19 Well, O(1) is still :D.