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

📅  最后修改于: 2021-10-26 07:01:00             🧑  作者: Mango

给定字符串str ,任务是使用给定的加密算法加密字符串。
字符串的第一个字符将在加密字符串重复一次第二个字符将重复两次,…,n字符将重复n次。

例子:

方法:初始化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


Javascript


输出:
geeeeekkkksssss

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程