Блог пользователя devsks

Автор devsks, 9 лет назад, По-английски

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
  • Проголосовать: не нравится

»
9 лет назад, # |
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 <cstring> or #include <string.h> . 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.