You are on page 1of 2

JAVA ASSIGNMENT6

1.WAP to create to accept a string from the user then count the total number of character present

There as well as print the characters in separate lines.

public class stringcharcount


{
public static void main(String[] args) {
String string = "Welcome to my java program";
int count = 0;

//Counts each character except space


for(int i = 0; i < string.length(); i++) {
if(string.charAt(i) != ' ')
count++;
}

//Displays the total number of characters in the string


System.out.println("Total number of characters in a string: " +
count);
}
}
Windows PowerShell

Copyright (C) Microsoft Corporation. All rights reserved.

Try the new cross-platform PowerShell https://aka.ms/pscore6

PS D:\Java\stringsprob> & 'C:\Program Files\Java\jdk-18.0.2.1\bin\java.exe' '-


agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=localhost:54669' '--enable-preview'
'-XX:+ShowCodeDetailsInExceptionMessages' '-cp'
'C:\Users\USER\AppData\Roaming\Code\User\workspaceStorage\9b59c7b2f996e2e43361986db680b
2a9\redhat.java\jdt_ws\stringsprob_c65c5103\bin' 'stringcharcount'

Total number of characters in a string: 22

PS D:\Java\stringsprob>

2. WAP to declare a suitable variable to assign “james-gosLING” and capitalise the 1st character
and rest in lower case, then finally print the output with suitable message.
public class casechange {
public static void main(String[] args) {
String str1="james-gosLING";
StringBuffer newStr=new StringBuffer(str1);

for(int i = 0; i < 1; i++) {

//Checks for lower case character


if(Character.isLowerCase(str1.charAt(i))) {
//Convert it into upper case using toUpperCase() function
newStr.setCharAt(i, Character.toUpperCase(str1.charAt(i)));
}
}
for(int i=1;i<str1.length();i++){
//Checks for upper case character
if(Character.isUpperCase(str1.charAt(i))) {
//Convert it into upper case using toLowerCase() function
newStr.setCharAt(i, Character.toLowerCase(str1.charAt(i)));
}
}
System.out.println("String after case conversion : " + newStr);
}
}
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Try the new cross-platform PowerShell https://aka.ms/pscore6

PS D:\Java> & 'C:\Program Files\Java\jdk-18.0.2.1\bin\java.exe' '-


agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=localhost:58288' '--enable-
preview' '-XX:+ShowCodeDetailsInExceptionMessages' '-cp'
'C:\Users\USER\AppData\Roaming\Code\User\workspaceStorage\a04c837b7bc6f5b577f6edfce
f350e4c\redhat.java\jdt_ws\strings_1d3111e9\bin' 'casechange'
String after case conversion : James-gosling
PS D:\Java>

You might also like