📌  相关文章
📜  用不同的字符替换字符串的每个字符

📅  最后修改于: 2022-05-13 01:57:06.831000             🧑  作者: Mango

用不同的字符替换字符串的每个字符

仅给定一个由小写字母组成的字符串str 。任务是用某个新字符。新字符属于$a-z$   (仅适用于小情况) str[i] 的替换由以下决定:

例子:

方法:

  1. 遍历字符串。
  2. 对于每个 i,找到需要用 str[i] 替换的字符。
  3. 将 str[i] 替换为该字符。

以下是上述方法的实现:

C++
// C++ program for Replace every character of a
// string by a different character
#include 
using namespace std;
 
// Function to manipulate the string
void manipulateString(string &str)
{
 
    // looping through each character of string
    for (int i = 0; i < str.length(); i++) {
 
        // storing integer ASCII value of
        // the character in 'asc'
        int asc = str[i];
 
        // 'rem' contains coded value which
        // needs to be rounded to 26
        int rem = asc - (26 - (str[i] - 'a'));
 
        // converting 'rem' character in range
        // 0-25 and storing in 'm'
        int m = rem % 26;
 
        // printing character by adding ascii value of 'a'
        // so that it becomes in the desired range i.e. a-z
        str[i] = (char)(m + 'a');
    }
}
 
// Driver code
int main()
{
 
    // Declaring str as 'geeksforgeeks'
    string str = "geeksforgeeks";
 
    manipulateString(str);
 
    cout << str;
 
    return 0;
}


Java
// Java program for Replace every character of a
// string by a different character
public class GFG {
 
    //Function to manipulate the string
    static void manipulateString(String str)
    {
 
        char[] str1 = str.toCharArray();
         
     // looping through each character of string
     for (int i = 0; i < str.length(); i++) {
 
         // storing integer ASCII value of
         // the character in 'asc'
          
         int asc = str1[i];
        
         // 'rem' contains coded value which
         // needs to be rounded to 26
         int rem = asc - (26 - (str1[i] - 97));
 
         // converting 'rem' character in range
         // 0-25 and storing in 'm'
         int m = rem % 26;
         
         // printing character by adding ascii value of 'a'
         // so that it becomes in the desired range i.e. a-z
         str1[i] = (char)(m + 'a');
             
     }
     
     String str2 = String.valueOf(str1);
     System.out.println(str2);
      
    }
 
    //Driver code
    public static void main(String[] args) {
         
        // Declaring str as 'geeksforgeeks'
         String str = "geeksforgeeks";
 
         manipulateString(str);
          
         
    }
}


Python 3
# Python 3 program for Replace every character of a
# string by a different character
 
# Function to manipulate the string
def manipulateString(str) :
 
    # looping through each character of string
    for i in range(len(str)) :
         
        # storing integer ASCII value of
        # the character in 'asc'
        asc = ord(str[i])
 
        # 'rem' contains coded value which
        # needs to be rounded to 26
        rem = asc - (26 - (ord(str[i]) - ord('a')))
 
        # converting 'rem' character in range
        # 0-25 and storing in 'm'
        m  = rem % 26
 
        #printing character by adding ascii value of 'a'
        # so that it becomes in the desired range i
        str[i] = chr(m + ord('a'))
 
    # join method join all individual
    # characters to form a string
    print(''.join(str))
 
# Driver code    
if __name__ == "__main__" :
 
    str = "geeksforgeeks"
 
    # convert string into list of characters
    str = list(str)
 
    # Function calling
    manipulateString(str)
 
  
# This code is contributed by ANKITRAI1


C#
// C# program for Replace every character of a
// string by a different character
using System;
public class GFG {
  
    //Function to manipulate the string
    static void manipulateString(String str)
    {
  
        char[] str1 = str.ToCharArray();
          
     // looping through each character of string
     for (int i = 0; i < str.Length; i++) {
  
         // storing integer ASCII value of
         // the character in 'asc'
           
         int asc = str1[i];
         
         // 'rem' contains coded value which
         // needs to be rounded to 26
         int rem = asc - (26 - (str1[i] - 97));
  
         // converting 'rem' character in range
         // 0-25 and storing in 'm'
         int m = rem % 26;
          
         // printing character by adding ascii value of 'a'
         // so that it becomes in the desired range i.e. a-z
         str1[i] = (char)(m + 'a');
              
     }
      
     String str2 = String.Join("",str1);
     Console.WriteLine(str2);
       
    }
  
    //Driver code
    public static void Main() {
          
        // Declaring str as 'geeksforgeeks'
         String str = "geeksforgeeks";
         manipulateString(str);
           
          
    }
}
// This code is contributed by PrinciRaj1992


Javascript


输出:
fbbnddvbfbbnd