Computer Applications
State the purpose and return data type of the following String functions:
- indexOf()
- compareTo()
Java String Handling
ICSE 2010
20 Likes
Answer
- indexOf() returns the index within the string of the first occurrence of the specified character or -1 if the character is not present. Its return type is int.
- compareTo() compares two strings lexicographically. Its return type is int.
Answered By
9 Likes
Related Questions
Define a class to accept a string and convert the same to uppercase, create and display the new string by replacing each vowel by immediate next character and every consonant by the previous character. The other characters remain the same.
Example:
Input : #IMAGINATION@2024
Output : #JLBFJMBSJPM@2024The following code to compare two strings is compiled, the following syntax error was displayed – incompatible types – int cannot be converted to boolean.
Identify the statement which has the error and write the correct statement. Give the output of the program segment.
void calculate() { String a = "KING", b = "KINGDOM"; boolean x = a.compareTo(b); System.out.println(x); }