Program to perform string operations | ZOHO

Program to perform string operations  | ZOHO

Question:

Program to perform String operations. ie Operator precedence is not considered

Logic:

  • Get the input from the user which contains numbers followed by operators.
  • Count of numbers is n means then operators must be n-1 otherwise input will be wrong. ie.,123-+(correct)
  • The operators and numbers doesn’t collide they must be in separated manner ie.,1234-+1/(wrong)
  • Take first two elements take first operator perform operation.
  • The result is assigned as first element and second element is next number in input.
  • At last print the result.

Program:


#include<stdio.h>
void main()
{
    //k is count of numbers
    //l is count of Operators
    //j act as a marker check collision ocuurs or not
    //d act as a marker for occurance of abnormal state
    //b, c contains first and second element for opraations 
    char a[30];
    int i,j=0,n,k=0,l=0,c,b,d=1;
    scanf("%s",&a);
    n=strlen(a);
    for(i=0;i<n;i++)
    {
        if(a[i]>=48&&a[i]<=58)
        {
            if(l==0)
            {
                k++;            //length of number
            }
            else
            {
            	j=1;           //wrong input ie 123*1/+
                break;
            }
        }
        else
        {
           l++;
        }
    }
    if(k!=l+1||j==1)           //wrong input ie 1234*/
    {
        printf("Input wrong");
    }
    else
    {
        l=0;
        b=a[0]-48;            //first element for arithmetic operation
        for(i=1;i<k;i++)
        {
            c=a[i]-48;        //second element for arithmetic operation
            switch(a[k+l])    //select the operator
            {                 //b is assigned as first element for every time
                case '+':
                    b=b+c;
                    break;
                case '-':
                    b=b-c;
                    break;
                case '*':
                    b=b*c;
                    break;
                case '/':
                    if(c==0)  //abnormal state of 1/0 error
                    {
                        printf("Abnormal state");
                        d=0;
                    }
                    b=b/c;
                    break;
            }
            if(d==0)
            {
                break;
            }
            l++;             //switch to next operator
        }
        if(d!=0)             //print the result
        {
            printf("%d",b);
        }
    }
}

You might also like:

To print snake pattern using two loop | ZOHO

Vignesh

A Computer Science graduate who likes to make things simpler. When he’s not working, you can find him surfing the web, learning facts, tricks and life hacks. He also enjoys movies in his leisure time.