0% found this document useful (0 votes)
28 views12 pages

21BEC2059 17 - 03 - 2022 (K. Pradeep)

The document contains 18 code examples showing different ways to manipulate and work with strings in Java. The examples demonstrate printing parts of strings, changing string values, getting string lengths, iterating through strings, concatenating strings, reversing strings, comparing strings, and checking for palindromes.

Uploaded by

aaeyantomar0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views12 pages

21BEC2059 17 - 03 - 2022 (K. Pradeep)

The document contains 18 code examples showing different ways to manipulate and work with strings in Java. The examples demonstrate printing parts of strings, changing string values, getting string lengths, iterating through strings, concatenating strings, reversing strings, comparing strings, and checking for palindromes.

Uploaded by

aaeyantomar0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Name: K.

Pradeep

Registration number: 21BEC2059

COMPUTER ASSINGNMENT – 3

1.

public class Main{

public static void main(String[] args){

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

System.out.println(cars[0]);

Output:

2.

public class Main{

public static void main(String[] args){

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

cars[0] = "Opel";

System.out.println(cars[0]);

}
}

Output:

3.

public class Main{

public static void main(String[] args){

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

System.out.println(cars.length);

Output:

4.

public class Main{

public static void main(String[] args){

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

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

System.out.println(cars[i]);
}

Output:

5.

public class Main{

public static void main(String[] args){

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

for (String i : cars){

System.out.println(i);

Output:
6.

public class Main{

public static void main(String[] args){

String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

System.out.println("The length of the txt string is: " + txt.length());

Output:

7.

public class Main{

public static void main(String[] args){

String txt = "Hello World";

System.out.println(txt.toUpperCase());
System.out.println(txt.toLowerCase());

Output:

8.

public class Main{

public static void main(String[] args){

String txt = "Please locate where 'locate' occurs!";

System.out.println(txt.indexOf("locate"));

Output:

9.

public class Main{

public static void main(String args[]){

String firstName = "John";


String lastName = "Doe";

System.out.println(firstName + " " + lastName);

Output:

10.

public class Main{

public static void main(String[] args){

String firstName = "John ";

String lastName = "Doe";

System.out.println(firstName.concat(lastName));

Output:

11.

public class Main{


public static void main(String[] args){

String txt = "It\'s alright.";

System.out.println(txt);

Output:

12.

public class Main{

public static void main(String[] args){

String txt = "Hello\nWorld!";

System.out.println(txt);

Output:

13.

public class Main{


public static void main(String[] args){

String txt = "Hello\rWorld!";

System.out.println(txt);

Output:

14.

public class Main{

public static void main(String[] args){

String txt = "Hello\tWorld!";

System.out.println(txt);

Output:

15.

public class Main{

public static void main(String[] args){


String txt = "Hel\blo World!";

System.out.println(txt);

Output:

16.Write a java program to reverse a string.

Code:

public class Reverse {

public static void main(String[] args) {

String str = "gnitseT erawtfoS";

StringBuilder stbuilder = new StringBuilder(str);

stbuilder.reverse();

System.out.println(stbuilder);

}
Output:

17.Write a java program to swap the two numbers.

Code:

public class SwapNumbers {

public static void main(String[] args) {

float first = 100, second = 200;

System.out.println("Before swap");

System.out.println("First number = " + first);

System.out.println("Second number = " + second);

float temporary = first;

first = second;

second = temporary;

System.out.println("After swap");

System.out.println("First number = " + first);

System.out.println("Second number = " + second);

}
Output:

18.Write a java program tofind whether a string or number is palindrome or


not.

Code:

public class PalindromeExample{

public static void main(String args[]){

int r,sum=0,temp;

int n=454;//It is the number variable to be checked for palindrome

temp=n;

while(n>0){

r=n%10; //getting remainder

sum=(sum*10)+r;

n=n/10;

if(temp==sum)

System.out.println("palindrome number ");

else

System.out.println("not palindrome");

}
}

Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy