📌  相关文章
📜  通过重复第i个字符加密字符串

📅  最后修改于: 2021-04-22 06:32:25             🧑  作者: Mango

给定字符串str ,任务是使用给定的加密算法对字符串进行加密。
字符串的1字符将被重复一次加密的字符串中,第2字符将被重复两次,…,n字符将被重复n次。
例如, str =“ abcd”将被加密为“ abbcccdddd”

例子:

方法:初始化CNT = 1,并开始遍历由字符字符串的字符。重复当前字符cnt的次数,然后更新cnt = cnt + 1并获得下一个字符。对字符串的每个字符重复这些步骤。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
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


输出:
geeeeekkkksssss