Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9054129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:36:18+00:00 2026-06-16T13:36:18+00:00

String hello = 44+ int result=0, top=-1; int []stack = new int[hello.length()]; for(int i=0

  • 0
String hello = "44+"
int result=0, top=-1;
int []stack = new int[hello.length()];
for(int i=0 ; i<hello.length() ; i++)
{
    char c = s.charAt(i);
    if(c >= '0' && c <= '9')
        stack[++top]=(int)(c-'0');
    else
    {
        int x=stack[top--]; //pop
        int y=stack[top--]; //pop
        switch(c)
        {
            case '+' : result = x + y;
                break;
            case '-' : result = y - x;
                break;
            case '*' : result = x * y;
                break;
            case '/' : result = y / x;
                break;
            default : result = 0;
        }
        stack[++top]=result; //push
    }
}
result = stack[top--]; //pop
return result;

44+ stores 8 -> return result happens and when I print it on the main I get 8 as a output.. which is correct

if I replace this code

    stack[++top]=(int)(c-'0');

with this one

    stack[++top]=(int)(c);

the output looks like this = 104.. the second code looks correct to me but it doesn’t give me the correct output

My questions are

  1. why c-'0' is used and not a c only ?
  2. why case '-' : result = y - x is used and not x - y ?
  3. why case '/' : result = y / x is used and not x / y ?

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T13:36:19+00:00Added an answer on June 16, 2026 at 1:36 pm

    A string (for example “44+”) contains a set of characters. Each character in a string is represented by a specific code or value. The string “ABC” is represented by the values 65,66,67 (in decimal). The string “ABC0123456789” is represented by the values 65,66,67,48,49,50,51,52,53,54,55,56,57 respectively. Therefore, to get the numerical value of the digit characters (‘0’ to ‘9’) you have to subtract 48 from the character’s code.

    Regarding your 2nd and 3rd questions: Y is the first number pushed on the stack and X is the second number pushed on the stack. Since the operation is supposed to be between the first and second digits, it should be Y-X and Y/X (it should also be Y+X and Y*X but the order doesn’t change the result in this case).

    Note that this code won’t work well with more than two digits or when the string isn’t formatted exactly as in the example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

String match = hello; String text = 0123456789hello0123456789; int position = getPosition(match, text); //
I have following simple program: import std.stdio; int main(string[] argv) { writeln(Hello, world!); return
I have a string: hello good old world and i want to upper case
Let's say I have this string: Hello &, how are you? I'm fine! Is
Which of these will achieve the correct result: (1) int X = 23; string
I have a string Hello I am going to I with hello am .
I have a string: Hello 7866592 this is my 12432 string and 823 i
Take the following Java string: Hello, World I want to take that string and
I have a string: Hello, my name is blah blah (goodbye) (hello) (oops) How
Say I have the following string: hello my car is red and my shoe

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.