Count of buttons pressed in a keypad mobile


Introduction

A string in C++ is an in-built storage structure used to contain digits, characters or even special symbols. Every string is associated with a definite size, specified by its length attribute. By default, the string positions begin with 0. The characters in string can be subjected to various types of manipulations −

  • New characters can be appended at the end of a string.

  • A character can be appended multiple times to the string.

In this article, we are going to develop a code that takes as input a string, and counts the number of times the keys have to be pressed in order to type that string onto the keypad mobile screen. An input array is also provided illustrating the number of times key presses occur for any particular character. Let us look at the following example to understand the topic better −

Sample Example

Example 1 − str − “abc”

Output − 6

For instance, the following example string, the total number of key presses are equivalent to 1+2+3 = 6.

In this article, we will create a solution to extract the character of the string one at a time and then extract its corresponding key presses from the input array. The counts are added each time to the sum variable.

Syntax

str.length()

length()

The length() method in C++ is used to compute the number of alphanumeric characters contained in the string. It can be used to count the number of alphanumeric characters , spaces as well as the digits.

Algorithm

  • An input string, str is accepted

  • A counter,count is maintained to store the number of times each character should be pressed to generate the string.

  • The length of the string is computed using the length() method, and stored in len variable

  • Each time the character at ith position is extracted, ch.

  • The counter is incremented the position by the number of times mentioned in the arr.

  • A decrementing loop initialised with the counter value is performed to append the extracted character to the output string.

  • Each time the count value is decremented.

  • After performing the required number of iterations with the character, the pointer is shifted on to the next character.

Example

The following C++ code snippet is used to create an encrypted string from the given input sample string −

//including the required libraries
#include <bits/stdc++.h>
using namespace std;
 
//storing thr reqd number of times a character should be pressed
const int arr[] = { 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 1, 2, 3, 4 };

//return the total number of times the button should be pressed
int numkeypresses(string str) {
   //initialising with count 
   int count = 0;
   //count the length of string 
   int len = str.length();
 
   // total key presses
   for (int i = 0; i < len; i++) {
      char ch = str[i];
      count+= arr[ch - 'a'];
   }
    
   cout << "Total number of key presses "<< count;
}
 
// Driver code
int main() {
   //input string
   string str = "tutorials";
   cout <<"Input String : "<< str << "\n" ;
   //calling the function to count the number of times key is pressed 
   numkeypresses(str);
 
   return 0;
}

Output

Input String − tutorials
Total number of key presses 21

Conclusion

Characters and integers operate using ASCII codes. Their interconversion can be easily simulated , for example, integer can be converted to its corresponding character equivalent by subtracting the character ‘a’. This results in the interconversion of its ASCII code which can be used for numerical manipulation operations of strings.

Updated on: 31-Jul-2023

71 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements