Encrypt a string by repeating i-th character i times


Introduction

A C++ string is a fixed sequence of alphanumeric characters. It is a continuous stream of occurring characters, which may be digits, characters or even special symbols. Every string is characterized by a definite length. The positions by which the characters are accessed begin with 0.

Strings can contain unique or repeated characters concatenated together. They can be subjected to various manipulations and concatenation operations.

In this article, we are going to develop a code that takes as input a string, and displays the encrypted string, wherein the first character is repeated 1 time, the second character is repeated 2 times. This procedure is repeated until the length of the string. Let us look at the following example to understand the topic better −

Sample Example

Example 1 − str − “g@m$

Output − g@@mmm$$$$

For instance, the following example string, contains special characters also, which are repeated according to the position of the character in the string.

In this article, we will create a solution to compute the number of times the character at a particular position should be repeated. The extracted character is then appended to the output string generated, until the count is exhausted.

Syntax

str.length()

length()

The size of the string can be captured by the length() method, which is used to return the alphanumeric characters and the special symbols contained within the string

Algorithm

  • An input string str is accepted as input

  • A counter, cnt is maintained to store the number of times each character should be repeated. It is initialized with the value of 0.

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

  • Each time the character at ith position is extracted.

  • The counter, cnt is computed by incrementing the position i by 1.

  • A decrementing loop initialized with the counter value is performed to append the extracted character to the output string, res

  • Each time the counter 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;
 
// Function to return the encrypted string
string encrypt(string str) {
   //counter to store the number of times a character is repeated
   int cnt = 0;
   //storing the encrypted string 
   string res = "";
   //computing the length of the string 
   int len = str.length();
 
   for(int i =0 ; i<len ; ) {
 
      //getting the count of the number of times the character will be repeated 
       cnt = i + 1;
 
      //repeating the current character
       while (cnt){
          //extracting the character at ith index
          char ch = str[i];
          //adding the character to output string 
          res += ch;
          //decrementing the count
          cnt--;
      }
      i++;
   }
 
   cout << "Encrypted string "<< res;
}
 
int main() {
   //taking a input string 
   string str = "heyy";
   cout << "Input string "<< str <<"\n";;
   //creating an encrypted string 
   encrypt(str);
 
   return 0;
}

Output

Input string heyy
Encrypted string heeyyyyyyy

Conclusion

The character positions in a C++ string start from by default the 0th index. A string is a dynamic length storage structure where the characters can be appended easily any number of times. String concatenation can easily be performed in C++ using the + operator. During each character addition the length of the string increases by 1.

Updated on: 31-Jul-2023

55 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements