• Post Reply Bookmark Topic Watch Topic
  • New Topic

A program to count no of characters in String  RSS feed

 
Pankaj Wakchaure
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Friends, Can anybody tell me the program that counts the no of characters in the string which is entered as command line argument. It is assumed that only one command line argument is entered. for example if user enters "press" as a command line argument then the output should be like : p1r1e1s2

Also (I know this is a java forum but) can anyone tell me the javascript function for validating an email-id.

Thanks,
Pankaj
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This sounds like homework, and there are a number of ways to proceed. Did you want to show us what you have, or explain your approach?
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well!!
you have said that you want to count the number of characters in a string but the string will be entered as command line argument try the below shown code.

First you take a buffered reader class(for this class import java.io.BufferedReader; as well as InputStreamReader) create its object ,use readline method to get input from command line.

BufferedReader buff;
buff=new BufferedReader(new InputStreamReader(System.in));

int i=0;
i=Integer.parseInt(buff.readline());
I am not that sure about its working because i have not tried it.....
i hope it helps you to atleast some extent.
 
Remko Strating
Ranch Hand
Posts: 893
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It could be more simple.

Just use the method toCharArray() of the String which will return an Array of Chars. You can now use the length property of the Array for determining the number of characters.

See the following link for code.

String functions
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Remko Strating:
It could be more simple.

Just use the method toCharArray() of the String which will return an Array of Chars. You can now use the length property of the Array for determining the number of characters.

See the following link for code.

String functions


In fact if you take a close look at the String API, you will find a method that directly returns the number of characters, without having to create a character array.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!