Encrypt a string by repeating i-th character i times
Given a string str, the task is to encrypt the string with the given encryption algorithm.
The 1st character of the string will be repeated once in the encrypted string, the 2nd character will be repeated twice, …, nth character will be repeated n times.
For example, str = “abcd” will be encrypted as “abbcccdddd”.
Examples:
Input: str = “geeks”
Output: geeeeekkkksssssInput: str = “abcd”
Output: abbcccdddd
Approach: Initialize cnt = 1 and start traversing the string character by character. Repeat the current character cnt number of times then update cnt = cnt + 1 and get to the next character. Repeat these steps for every character of the string.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the encrypted string string encryptString(string str, int n) { int i = 0, cnt = 0; string encryptedStr = ""; while (i < n) { // Number of times the current // character will be repeated cnt = i + 1; // Repeat the current character // in the encrypted string while (cnt--) encryptedStr += str[i]; i++; } return encryptedStr; } // Driver code int main() { string str = "geeks"; int n = str.length(); cout << encryptString(str, n); return 0; } |
Java
// Java implementation of the approach class GFG { // Function to return the encrypted String static String encryptString(String str, int n) { int i = 0, cnt = 0; String encryptedStr = ""; while (i < n) { // Number of times the current // character will be repeated cnt = i + 1; // Repeat the current character // in the encrypted String while (cnt-- >0) encryptedStr += str.charAt(i); i++; } return encryptedStr; } // Driver code public static void main(String[] args) { String str = "geeks"; int n = str.length(); System.out.println(encryptString(str, n)); } } // This code has been contributed by 29AjayKumar |
Python3
# Python3 implementation of the approach # Function to return the encrypted string def encryptString(string, n): i, cnt = 0, 0 encryptedStr = "" while i < n: # Number of times the current # character will be repeated cnt = i + 1 # Repeat the current character # in the encrypted string while cnt > 0: encryptedStr += string[i] cnt -= 1 i += 1 return encryptedStr # Driver code if __name__ == "__main__": string = "geeks" n = len(string) print(encryptString(string, n)) # This code is contributed # by Rituraj Jain |
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the encrypted String
static String encryptString(String str, int n)
{
int i = 0, cnt = 0;
String encryptedStr = “”;
while (i < n)
{
// Number of times the current
// character will be repeated
cnt = i + 1;
// Repeat the current character
// in the encrypted String
while (cnt-- >0)
encryptedStr += str[i];
i++;
}
return encryptedStr;
}
// Driver code
static public void Main ()
{
String str = “geeks”;
int n = str.Length;
Console.WriteLine(encryptString(str, n));
}
}
// This code is contributed by ajit
PHP
<?php // PHP implementation of the approach // Function to return the encrypted string function encryptString($str, $n) { $i = 0 ; $cnt = 0; $encryptedStr = ""; while ($i < $n) { // Number of times the current // character will be repeated $cnt = $i + 1; // Repeat the current character // in the encrypted string while ($cnt--) $encryptedStr .= $str[$i]; $i++; } return $encryptedStr; } // Driver code $str = "geeks"; $n = strlen($str); echo encryptString($str, $n); // This code is contributed by Ryuga ?> |
geeeeekkkksssss
Recommended Posts:
- Decrypt a string encrypted by repeating i-th character i times
- Replace every character of string by character whose ASCII value is K times more than it
- Queries to find the last non-repeating character in the sub-string of a given string
- Queries to find the first non-repeating character in the sub-string of a string
- Find the last non repeating character in string
- First non-repeating character using one traversal of string | Set 2
- Given a string, find its first non-repeating character
- Maximum consecutive repeating character in string
- Print the string after the specified character has occurred given no. of times
- Check if max occurring character of one string appears same no. of times in other
- Encrypt the given string with the following operations
- Encrypt a string into the Rovarspraket (The Robber Language)
- K'th Non-repeating Character
- Encrypt string with product of number of vowels and consonants in substring of size k
- Find the first non-repeating character from a stream of characters
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



