6.21 LAB: Remove all non alpha characters Write a program that removes all non alpha characters from the given input. Ex: If the input is: -Hello, 1 world$! the output is: Helloworld 296430.1461558.qx3zqy7 LAB 6.21.1: LAB: Remove all non alpha characters 2/ 10 АCTIVITY

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter3: Input/output
Section: Chapter Questions
Problem 12SA: 12. What is the output of the following program? (2, 3, 6, 8) #include <iostream> #include...
icon
Related questions
Question
100%

I'm unable to use the method replaceAll because we have not touch upon it yet, so since the chapter cover loops, here are my 2 codes

 

(Code 1) 

import java.util.Scanner;

public class LabProgram {
public static void main(String[] args) {
/* Type your code here. */Scanner scnr = new Scanner(System.in);
String str= scnr.next();
String newStr="";

for (int i=0; i < str.length(); i++) {
if (str.charAt(i) == '@')
newStr+="";
else if (str.charAt(i) == '1')
newStr+="";
else if (str.charAt(i) == '!')
newStr+="";
else if (str.charAt(i) == '-')
newStr+="";
else if (str.charAt(i) == '$')
newStr+="";
else if (str.charAt(i) == '.')
newStr+="";
else if (str.charAt(i) == ' ')
newStr+="";
else
newStr= str.charAt(i);
}
System.out.println(newStr);
}
}

 

(Code 2) 

It's the same as the 1st code but I removed the charAt(i) to newStr as in:

newStr=str;

System.out.println(newStr);

This code displayed the string but no nonalpha characters were removed. 

uDiit T mode
submit your program for auto-grading.
Submit for grading
Signature of your work
What is this?
3/7.. U|2|2 |2|2|0|0|0| 0|2|0 ..3/7
Latest submission - 8:04 PM PST on 03/07/21
Total s
Download this
Failed to compile
LabProgram.java:25: error: incompatible types: char cannot be converted to String
newStr= str.charAt(i);
1 error
Transcribed Image Text:uDiit T mode submit your program for auto-grading. Submit for grading Signature of your work What is this? 3/7.. U|2|2 |2|2|0|0|0| 0|2|0 ..3/7 Latest submission - 8:04 PM PST on 03/07/21 Total s Download this Failed to compile LabProgram.java:25: error: incompatible types: char cannot be converted to String newStr= str.charAt(i); 1 error
6.21 LAB: Remove all non alpha characters
Write a program that removes all non alpha characters from the given input.
Ex: If the input is:
-Hello, 1 world$!
the output is:
Helloworld
296430.1461558.qx3zqy7
LAB
6.21.1: LAB: Remove all non alpha characters
2/10
ACTIVITY
Transcribed Image Text:6.21 LAB: Remove all non alpha characters Write a program that removes all non alpha characters from the given input. Ex: If the input is: -Hello, 1 world$! the output is: Helloworld 296430.1461558.qx3zqy7 LAB 6.21.1: LAB: Remove all non alpha characters 2/10 ACTIVITY
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Developing computer interface
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning