Home > Blockchain >  how can i create a new line using if else
how can i create a new line using if else

Time:04-21

The code runs but if the user input the * # 0 nothing happens. the purpose of this is that if the user input * it will create a newline and the # it will create a space and 0 it will print 0. The expected output is this.

Program Input: 4433 555 555 666 9666 777 555 3 222 9992 #555 8888777 *

Expected output:

HELLO

WORLD

CYA L8R

#include <stdio.h>
 

char printSentence(char* str)
{
    // Store the mobile keypad mappings
        char nums[][6] = {
                "", "@.?1", "ABC2", "DEF3", "GHI4", "JKL5",
                "MNO6", "PQRS7", "TUV8", "WXYZ9"
        };

 
    // Traverse the string str
    int i = 0;
    while (str[i] != '\0') {
 

        // Stores the number of
        // continuous clicks
        int count = 0;
        char code;
        // Iterate a loop to find the
        // count of same characters
        while (str[i   1] && str[i] == str[i   1]) {
            

 
            // 2, 3, 4, 5, 6 and 8 keys will
            // have maximum of 4 letters
            if (count == 3 && ((str[i] >= '2' && str[i] <= '6') || (str[i] == '8')))
                break;
                
                //if pressed it will create a newline
                if(code == '*')
                       return '\n';
                //if pressed it will create a space
                if(code == '#')
                       return ' ';
                //if pressed it will print 0
                if(code == '0')
                        return '0';
                
 
            // 7 and 9 keys will have
            // maximum of 5 keys
            else if (count == 4 && (str[i] == '7' || str[i] == '9'))
                break;
            count  ;
            i  ;
 
            // Handle the end condition
            if (str[i] == '\0')
                break;
        }
 
        // Check if the current pressed
        // key is 7 or 9
        if (str[i] == '7' || str[i] == '9') {
            printf("%c",nums[str[i] - 48][count % 4]);
        }
 
        // Else, the key pressed is
        // either 2, 3, 4, 5, 6 or 8
        else {
            printf("%c", nums[str[i] - 48][count % 3]);
        }
        i  ;
        
    }
}
 
// main function
int main()
{
    char a[100];

    printf("Input A program: ");
    scanf(" %[^\t\n]s", a); //Read a space input
    printSentence(a);
    return 0;
}

CodePudding user response:

        // Else, the key pressed is
        // either 2, 3, 4, 5, 6 or 8
        else {
            printf("%c", nums[str[i] - 48][count % 3]);
        }

The comment is wrong. Anything can be in str[i] at this point, not simply 2, 3, 4, 5, 6, or 8.

Try this

    // Else, it is not a repeated key, not '7', not '9'
    else {
        if (strchr("234568", str[i])) printf("%c", nums[str[i] - 48][count % 3]);
        else if (str[i] == '*') putchar('\n');
        else if (str[i] == '#') putchar(' ');
        else if (str[i] == '0') putchar('0');
        else (void)0; // do nothing, str[i] can be <space>, or '1' or some other input
    }
  •  Tags:  
  • c
  • Related