Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java problem. Write a program that manipulates two strings. The program inputs t

ID: 3842476 • Letter: J

Question

Java problem. Write a program that manipulates two strings. The program inputs two strings (string1 and string2) and a character (char1) and an integer (position) to represent a character position within a string. The program will display the following:
1. Whether string1 is less, greater or equal to string2
2. string1 in upper case
3. string2 in lower case
4. The number of characters in string1
5. The first character in string2
6. The number of occurrences that char1 is contained in string1.
Hint: use a for loop and charAt
7. The character that is in the position of string1.
Turn in a run of the program that uses your first and last names as the strings.
Use at least two methods with the following headers:
int countOccurrences(String s, char c) // to answer #6
char showChar(String s, int pos)// to answer #7

Explanation / Answer

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Dell
*/
public class stringManipulation {
  
static int countOccurences(String s, char c){
  
int ctr=0,i,len;
len=s.length();
  
for(i=0;i<len;i++){
if(s.charAt(i)==c)
ctr++;
}
  
return ctr;
  
}
  
static char showChar(String s, int pos){
  
return s.charAt(pos);
  
}
  
  
  
  
public static void main(String args[]){
String str1="shanu",str2="jain";
char ch='a';
int pos=3;
  
  
//1. to compare two strings by using compareTo()
if(str1.compareTo(str2)==0)
System.out.println("Equal strings");
else
if(str1.compareTo(str2)<0)
System.out.println(str1+" is lesser than "+str2);
else
System.out.println(str1+" is greater than "+str2);
  
//. 2. string1 in uppercase
  
System.out.println(str1.toUpperCase());
  
// 3. string 2 in lower case
System.out.println(str1.toLowerCase());

//4. length of string1
System.out.println(str1.length());

//5. first character in string2
System.out.println(str1.charAt(0));

// 6. No. of occurences of char1 in string1
System.out.println(countOccurences(str1,ch));

//7. character at the given position
System.out.println(showChar(str1,pos));
  
  
  
  
}
  
  
  
  
  
  
  
  
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote