GfG QA is closing soon... Please refer PRACTICE or GfG now on wards !!

Why am I not able to get rid of spaces in array??

Here is my code. I am attempting to get rid of the spaces in original input and then calculate the mathematical sentence. I can't seem to get rid of the spaces.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define SIZE 30

int main()
{
    char mthSent [SIZE];
    char noSpSent [SIZE];
    int ans;
    int i=0;
    int j=0;



    printf("Please enter a mathematical expression using digits 0-9 and operands +,-,*,/");
    fgets(mthSent, 30, stdin);

    ans= mthSent[i]-'0';
    i++;
    while (mthSent[j] != '\n')
    {
        if (mthSent[j] == ' ')
        {
            int temp = j + 1;
            if (mthSent[temp] != '\n')
            {
                while (mthSent[temp]== ' '&& mthSent [temp] != '\n')
                {
                    if (mthSent[temp] == ' ')
                    {
                        j++;
                    }
                    temp++;
                }
            }
        }
        noSpSent[i] = mthSent [j];
        j++;
        i++;
    }

    while(i != '\n')
    {

            switch(noSpSent[i])
            {
        case '+':
            i++;
            ans += (noSpSent[i]-'0');
            break;
        case '-':
            i++;
            ans -= (noSpSent[i]-'0');
            break;
        case '*':
            i++;
            ans *= (noSpSent[i]-'0');
            break;
        case '/':
            i++;
            ans /= (noSpSent[i]-'0');
            break;


            }

        i++;


    }
    printf("The answer to the mathematical sentence you have provided is %d\n", ans);
    return 0;
}

 

asked Mar 8, 2017 by anonymous
...