Computer Science, asked by rahamtunnisasamrin, 4 days ago

CharactersOrSumDigits Given a string input1, having alphabets or digits or both, find the sum of digits or extract only the characters depending on input2. Prototype string findCharsOrDigitSum(string input, int input2) input1 - String having alphabets or digits or both input2 - Either 0 or 1 If input2 = 0, find the sum of digits from string input1 If input2 = 1, extract only ALPHABETS from string input1 Rules: If there are no DIGITS in the given string and input2 is o, result would 11 be ZERO If there are no ALPHABETS in the given string and input2 is 1, result would again be ZERO • If the given string is null (or empty) and if input2 is either 0 or 1. result would be NULL • No special characters or spaces are allowed in string input1​

Answers

Answered by vishwa11747
1

Answer:

idk

Explanation:

Answered by ravilaccs
0

Answer:

The program is coded based on the given statement

Explanation:

For calculating the weight of the string,

  • Weight of all alphabetic characters that appear in the string should be added
  • Weight of vowels that appear in the string should either be ignored OR added depending upon a specified option
  • All non-alphabetic characters in the string should be ignored
  • Weight of each letter is its position in the English alphabet system, i.e. weight of a=1, weight of b=2, weight of c=3, weight of d=4, and so on….weight of y=25, weight of z=26.
  • Weight of Upper-Case and Lower-Case letters should be taken as the same, i.e. weight of A=a=1, weight of B=b=2, weight of C=c=3, and so on…weight of Z=z=26.

Example1:

Let us assume the word is “Hello World!!” and vowels are to be ignored.

Weight of “Hello World!!” = 8+0+12+12+0+0+23+0+18+12+4+0+0 = 89

Note: Note that weight of vowels is ignored. Also note that the weight of non-alphabetic characters such as space character and ! is taken as zero.

Example2:

Let us assume the word is “Hello World” and vowels are to be included.

Weight of “Hello World!!” = 8+5+12+12+15+0+23+15+18+12+4+0+0 = 124

Note: Note that weight of vowels is included. Also note that the weight of non-alphabetic characters such as space character and ! is taken as zero.

The function will accept two input parameters  input1 and input2,

where,

input1 represents the string whose weight needs to be calculated, and,

input2 represents the option specifying whether or not the weight of vowels should be included.

If input2 is 0, vowels that appear in the string should be ignored.

If input2 is 1, the weight of vowels that appear in the string should also be added.

The function is expected to calculate and return the weight of the string.

Example1:

input1 = “Hello World”

input2 = 0

output1 = 89

Example2:

input1 = “Hello World”

input2 = 1

output1 = 124

Example3:

input1 = "All Zebras are Black & White!!"

input2 = 0

output1 = 186​

Example4:

input1 = "All Zebras are Black & White!!"

input2 = 1

output1 = 214

Program

import java.io.*;

import  java.util.*;

class UserMainCode

{

public int weightOfString(String input1,int input2)

{

String str=input1.toUpperCase();

       int sum=0;

       for(int i=0;i<input1.length();i++)

       {

         if(input2==0)

         {

     if(str.charAt(i)=='A' || str.charAt(i)=='E' ||str.charAt(i)=='I' || str.charAt(i)=='O' ||                     str.charAt(i)=='U' || !Character.isLetter(str.charAt(i)))

             {

                 continue;

             }

             else

             {

                 int a=str.charAt(i)-64;

                 sum+=a;

             }

         }

         else

         {

             if(!Character.isLetter(str.charAt(i)))

                 continue;

             else

             {

                 int a=str.charAt(i)-64;

                 sum+=a;

             }

         }

       }

       return sum;

   }

}

Similar questions