You are not logged in.
Pages: 1
Different Type of Problem
Write a program that compose message in the same fashion as using mobile keypad. The program must receive integer input. When use enter 2 the program outputs letter ‘A’ and if 2 is pressed twice, the letter ‘A’ will disappear from screen and character ‘B’ will appear and if 2 is pressed thrice the letter ‘B’ will disappear and Letter C will appear. To enter ‘AA’ you have to enter number ‘2’ twice with delay. Delay must be as smaller as it is usually in the mobile.
To check a delay between two key press use the following library
#include <time.h>
To get the current time use following function
time(0)
This function will return numeric value of time. You can compute the time difference by getting time at both key press events and test for a fixed time delay to check whether the key press are consecutive or not.
If you want to clear screen use following statement
System(“CLS”);
Similarly find the associated letters with numbers
Requirement.
You cannot use string and character array. Only primitive data types are allowed. You are allowed to use header files that is mentioned or may use other libraries.
Malik
Offline
hi Zeeshan 01
You haven't said what you want from this post. Also what coding language?
Bob
Children are not defined by school ...........The Fonz
You cannot teach a man anything; you can only help him find it within himself..........Galileo Galilei
Sometimes I deliberately make mistakes, just to test you! …………….Bob
Offline
C ++ language.
Malik
Offline
Anyone there to solve this problem by program?.
Malik
Offline
Anyone there to solve this problem by program?.
Not if you disinclude hi-tech data structures cuz theres no reason to do that >:C
Offline
Maybe this code will help you:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
// Function prototypes
void displayCharacter(int keyPressCount, int key);
void clearScreen();
char getCharacter(int key, int pressCount);
int main() {
int key;
int keyPressCount = 0;
time_t lastPressTime = 0;
time_t currentTime;
const int delay = 2; // Time delay in seconds to distinguish key presses
while (1) {
printf("Enter a key (2-9) or 0 to exit: ");
key = getchar();
while (getchar() != '\n'); // Clear input buffer
if (key == '0') {
break;
}
currentTime = time(NULL);
if (key >= '2' && key <= '9') {
if (key == lastPressTime) {
if (difftime(currentTime, lastPressTime) < delay) {
keyPressCount++;
} else {
keyPressCount = 1;
}
} else {
keyPressCount = 1;
}
lastPressTime = key;
clearScreen();
displayCharacter(keyPressCount, key - '0');
} else {
printf("Invalid key. Press a number between 2 and 9.\n");
}
}
return 0;
}
void displayCharacter(int keyPressCount, int key) {
char character = getCharacter(key, keyPressCount);
printf("%c\n", character);
}
char getCharacter(int key, int pressCount) {
char result = '\0';
switch (key) {
case 2: result = "ABC"[pressCount - 1]; break;
case 3: result = "DEF"[pressCount - 1]; break;
case 4: result = "GHI"[pressCount - 1]; break;
case 5: result = "JKL"[pressCount - 1]; break;
case 6: result = "MNO"[pressCount - 1]; break;
case 7: result = "PQRS"[pressCount - 1]; break;
case 8: result = "TUV"[pressCount - 1]; break;
case 9: result = "WXYZ"[pressCount - 1]; break;
}
return result;
}
void clearScreen() {
system("CLS");
}
Offline
Pages: 1