Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strings.java #72

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions Strings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import java.util.*;

public class Strings {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter name");
String name = sc.nextLine();
System.out.println("Your name is "+name);

//concatenation
System.out.println("Enter first name");
String first = sc.next();
System.out.println("Enter last name");
String last = sc.next();
String fullname = first+" "+last;
System.out.println("Your full name is"+fullname);
System.out.println(fullname.length());

//charAt

for (int i = 0; i < fullname.length(); i++) {
System.out.println(fullname.charAt(i));
}


//comparison function

if (first.compareTo(last)==0){
System.out.println("Equal");
} else if (first.compareTo(last)>0){
System.out.println("Strings are not equal and greater than other name");
}else {
System.out.println("Strings are not equal and second string is greater than 1st string");
}

//Substring

String Sub = fullname.substring(5, fullname.length());
System.out.println(Sub);


//Check for substring existence
System.out.println("Enter substring you want to check");
String x = sc.next();
if (name.contains(x)) {
System.out.println("Substring Found");
}


//Replace Substrings
System.out.println("Enter string you want to replace with");
String y = sc.next();
String replacedStr = name.replace(x, y);
}
}