Home > Software engineering >  Last character of numeric string being removed, how to fix this problem in C?
Last character of numeric string being removed, how to fix this problem in C?

Time:12-13

I'm trying to solve a problem with replacing letters with numbers. The user will enter a string, in case there are letters, I must substitute the corresponding number, and in case there are * # - symbols, I must simply remove them.

However, I am facing an issue. When the user types only a numeric string, the last character of this string is being removed, which cannot happen. This can only happen if there are letters or symbols in the string.

Source

#include <stdio.h>
#include <string.h>

void alterChars(char phrase[])
{
    int i, dashes = 0;
    
    for (i = 0; phrase[i] != '\0'; i  )
    {
        if (phrase[i] == 'A' || phrase[i] == 'B' || phrase[i] == 'C')
        {
            phrase[i] = '2';
        }

        if (phrase[i] == 'D' || phrase[i] == 'E' || phrase[i] == 'F')
        {
            phrase[i] = '3';
        }

        if (phrase[i] == 'G' || phrase[i] == 'H' || phrase[i] == 'I')
        {
            phrase[i] = '4';
        }

        if (phrase[i] == 'J' || phrase[i] == 'K' || phrase[i] == 'L')
        {
            phrase[i] = '5';
        }

        if (phrase[i] == 'M' || phrase[i] == 'N' || phrase[i] == 'O')
        {
            phrase[i] = '6';
        }
        
        if (phrase[i] == 'P' || phrase[i] == 'Q' || phrase[i] == 'R' || phrase[i] == 'S')
        {
            phrase[i] = '7';
        }
        
        if (phrase[i] == 'T' || phrase[i] == 'U' || phrase[i] == 'V')
        {
            phrase[i] = '8';
        }
        
        if (phrase[i] == 'W' || phrase[i] == 'X' || phrase[i] == 'Y' || phrase[i] == 'Z')
        {
            phrase[i] = '9';
        }
        
        if (phrase[i] == '*' || phrase[i] == '#' || phrase[i] == '-')
        {
            dashes  ;
        }
        else if (dashes > 0)
        {
            phrase[i - dashes] = phrase[i];
        }
    }

    phrase[strlen(phrase)-1] = '\0';
    
    printf("%s\n", phrase);
}

int main()
{
    char phrase[300];

    while (!feof(stdin))
    {
        scanf(" %[^\n]s", phrase);
        alterChars(phrase);
    }

    return 0;
}

Any tips will be valuable. You can access the problem to see where the error is occurring. Anyway, it's on the last entry, at number 190. It is being printed 19, but in fact it should be printed 190, because the removal of characters should only occur when there are letters, or symbols.

Examples

Input: 333-PORTO
Output: 33376786

The problem:

Input: 190
Output: 19

CodePudding user response:

With your "generous" scanning, i.e. practically no expected syntax, you can simply loop while scanning is successful.

while (1==scanf(" )9[^\n]", phrase)) alterChars(phrase);

That fixes the problem with your while condition ( see Why is “while ( !feof (file) )” always wrong? ).

Then you want to read each line until either newline or terminator:

for (i = 0; phrase[i] != '\0' && phrase[i] != '\n'; i  )

And you want to force-terminate with respect to the dashes.

phrase[strlen(phrase)-dashes] = '\0';

and you get the desired output - and in only one line.... ;-)

The explanation is, if you always terminate as with 1 dash, then it works for 1 dash, fails by cutting too much for zero dashes and does other unwanted stuff for more than one dash, i.e. repeat the last few characters.
It is after all unrelated to "numeric only".

I have incorporated this good input by chux:

scanf(" %[^\n]s" is bad as it has not width limit and 2) the s is pointless.

  • Related