0% found this document useful (0 votes)
6 views5 pages

OOPS2

The document provides Java code examples demonstrating how to find the length of strings, check the capacity of StringBuffer, extract specific characters from a string, and convert the case of characters in a string. It includes methods for substring extraction and character manipulation using built-in functions. Additionally, it outlines an algorithm for case conversion and presents a complete program for this functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

OOPS2

The document provides Java code examples demonstrating how to find the length of strings, check the capacity of StringBuffer, extract specific characters from a string, and convert the case of characters in a string. It includes methods for substring extraction and character manipulation using built-in functions. Additionally, it outlines an algorithm for case conversion and presents a complete program for this functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

8.

(a)FINDING LENGTH AND CAPACITY OFTHE STRING

(i)
public class LengthExample{
public static void main(String args[]){
String s1="javatpoint";
String s2="python";
System.out.println("string length is: "+s1.length());//10 is the length of javatp
oint string
System.out.println("string length is: "+s2.length());//6 is the length of python
string
}}

ii) public class StringBufferCapacityExample2 {


public static void main(String[] args) {
StringBuffer sb = new StringBuffer("string buffer");
System.out.println("capacity: " + sb.capacity());
sb = new StringBuffer("A");
System.out.println("capacity: " + sb.capacity());
}
}
class GFG {

// Function to get the specific character

public static char

getCharFromString(String str, int index)

return (char)str.codePointAt(index);

// Driver code

public static void main(String[] args)

// Get the String

String str = "GeeksForGeeks";

// Get the index

int index = 5;

// Get the specific character

char ch = getCharFromString(str, index);

System.out.println("Character from " + str

+ " at index " + index

+ " is " + ch);

}
public class Demo {
public static void main(String[] args) {
String str = "pqrstuvw";
System.out.println("String: "+str);
// range from 3 to 6
String strRange = str.substring(3, 6);
System.out.println("Substring: "+strRange);

ALGORITHM:

o STEP 1: START
o STEP 2: DEFINE string str = "Great Power".
o STEP 3: DEFINE newstr as StringBuffer object .
o STEP 4: SET i=0. REPEAT STEP 5 to STEP 6 UNTIL i<str.length().
o STEP 5: IF lower-case character encountered then CONVERT them
in upper-case using built-in function
else
IF upper-case character encountered then CONVERT them in lower-
case characters using built-in function.
o STEP 6: i=i+1
o STEP 7: PRINT newstr.
o STEP 8: END

PROGRAM:

public class changeCase {

public static void main(String[] args) {

String str1="Great Power";

StringBuffer newStr=new StringBuffer(str1);

for(int i = 0; i < str1.length(); i++) {

if(Character.isLowerCase(str1.charAt(i))) {

newStr.setCharAt(i, Character.toUpperCase(str1.charAt(i)));

else if(Character.isUpperCase(str1.charAt(i))) {

newStr.setCharAt(i, Character.toLowerCase(str1.charAt(i)));

System.out.println("String after case conversion : " + newStr);

OUTPUT:

You might also like