Hello I need help with this task. Make a program that takes input and checks to
ID: 3759056 • Letter: H
Question
Hello I need help with this task.
Make a program that takes input and checks to see if it matches a rule. For example:If the format was
(number +/-/*/// num)
so make the program store an input as a string, and create a counter object. Then have the program iterate through the characters of the string. If a character was '(' and the counter was 0 increase the counter and added that character to an output string. Then look for if a following character was a digit and the counter was 1. If this is true then add the character and increase the counter. Continue this for the whole string.So if t by the end, the counter reaches 5 then it has found all of the characters it was looking for so output the output string. If not, output and error message.
Can use java or c++. I was trying with java first.
Explanation / Answer
import java.util.*;
class char_count
{
public static void main(String args[])
{
Scanner o=new Scanner(System.in);
String s= o.nextLine();
String s1="";
int i,d=0;
char c;
for(i=0;i<s.length();i++)
{
c=s.charAt(i);
if(c=='(')
{
d++;
s1=s1+c;
}
if(d>=1 && Character.isDigit
(c))
{
d++;
s1=s1+c;
}
else if(c==')')
{
break;
}
}
System.out.println(s1);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.