English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I have some practice problems I am working on this weekend for a programming meet at my high school. One of the problems wants me to count vowels and consonants. I have this part done without any problems. The next step is to count the whole string without using the .length method. I am not sure how to do this. I think I have to use an array here but not sure how to do it. From what the directions say I have to have all user defined methods. I tried to call my coach for help but his wife said he was not going to be home this weekend. Thanks for the help in advance.

2007-02-24 04:52:35 · 3 answers · asked by thunderbbolt 2 in Computers & Internet Programming & Design

3 answers

You forgot to specify the language.

I think you can put the string in an array, and then check for the null termination using a for while and a counter. Kind of like this in java. The concept is the same for other languages.


int counter =0;
while(someArray[counter] != NULL)
{
++counter;
}
System.out.println("the length ="+counter);

2007-02-24 05:01:42 · answer #1 · answered by mr_gees100_peas 6 · 0 0

I assume your professor wants you to make a method with the signature /***********************/ public int getLen(String input) /***********************/ that calls no further methods? You will find that all solutions will call at least 1 method, because the only things you can do without calling methods in Java is casting, variable assignment, basic arithmetic, and reading object fields. Unfortunately, neither String nor its superclasses have relevant fields to read. You can not even initialize objects without calling a constructor method. Also, if your professor is talking about reading input from the console, you need methods to read input. Please ask your professor for clearer instructions--it seems he does not know what he is asking. However, if you are merely limited from using String methods, there are many ways. If that is the case, please revise the question as necessary.

2016-05-24 06:08:05 · answer #2 · answered by Anonymous · 0 0

hmmm get that string into a character array

then use a forloop till end of string and increment a variable.
the end of string is mostly denoted by \0 character in systems
some other systems may use different notations.
check your manual for the exact details

2007-02-24 04:57:28 · answer #3 · answered by Blistika Barnacles 2 · 0 0

fedest.com, questions and answers