+ 2
How to count characters in a string ?
suppose "abcdef" (exclude the quotations) is a string! so how can i find the number of characters of it ! (in this case the characters are 6) ! anyone?đ
10 Respostas
+ 7
language?
+ 4
# Using Python..
You can convert the string into list so as to make each character countable..
And use the len() function to obtain the length of the string; now in a list form, (number of chars in the string) afterwards...
i.e.
string = "abcdef"
list(string)
print(len(string))
+ 4
String str = "afrinish";
System.out.print(str.length());
+ 3
i did đ
+ 3
Here is a C++ example
https://code.sololearn.com/cTDzkSCe0540/?ref=app
+ 3
This is a Java example
https://code.sololearn.com/c1M1mLN6Oz7Q/?ref=app
+ 2
Please put the language of your question in the tags.
+ 1
java đ
+ 1
Let's use Python to help us.
Python has an inbuilt sum function, to find the number of characters in a string or table.
This would be (kind of) equivalent to C++/C#'s len function.
alphabet = "abcdefg"
sum(alphabet)
||
int num = alphabet.length();
You may want to revise on your language documentation - my knowledge has some inaccuracies... hope i helped.
edit0
Well now I know I helped.
Just remember that the length method can not be ran on non-string type objects, so... (excluding arrays - under differrent circumstances) intergers and floats; there's not much that it can't affect...
+ 1
i tried your method ghostwalker13...it worked!
import java.util.*;
public class Program
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String ar= sc.next();
int num=ar.length();
System.out.print(num);
}
}