📌  相关文章
📜  Java使用字符和数字生成字符串的不同方式

📅  最后修改于: 2021-09-04 09:42:03             🧑  作者: Mango

给定一个数字num和 String str ,任务是通过使用数字的索引值从字符串提取字符来生成新的 String 。

例子:

方法 1:使用两次遍历

  1. 获取字符串和数字以生成新字符串。
  2. 初始化一个存储最终答案的变量。
  3. 将给定的数字转换为字符串。
  4. 从头到尾遍历循环。
  5. 获取数字的最后一位并将第 k 个位置字符添加到变量 answer 中。
  6. 以相反的顺序遍历循环并将第j 个位置字符添加到变量finalAnswer 中
  7. 现在,打印结果。
Java
// Java program to generate String by using
// Strings and numbers
class GFG {
  
    // Function to generate the new String
    public static String generateNewString(String str,
                                           int num)
    {
        // Initialize a variable that
        // stores the final anser.
        String finalAnswer = "";
  
        String answer = "";
  
        // Converting the given numbers
        // into string
        String index = String.valueOf(num);
  
        // Traverse the loop from start to end
        for (int i = 0; i < index.length(); i++) {
  
            // Getting last digit of numbers
            int k = num % 10;
  
            // Adding kth position character
            // into the variable answer
            answer = answer + str.charAt(k);
            num = num / 10;
        }
  
        // Traverse the loop in reverse order
        for (int j = answer.length() - 1; j >= 0; j--) {
  
            // Adding jth position character
            // into the variable finalAnswer
            finalAnswer = finalAnswer + answer.charAt(j);
        }
  
        // Return the finalAnswer
        return finalAnswer;
    }
  
    // Driver Code
    public static void main(String args[])
    {
  
        // Given String str
        String str = "GeeksforGeeks";
  
        // Given Number num
        int num = 858;
  
        // Printing the result
        System.out.println(generateNewString(str, num));
    }
}


Java
// Java program to generate String by using
// Strings and numbers
class GFG {
  
    // Function to generate the new String
    public static String generateNewString(String str,
                                           int num)
    {
        // Initialize a variable that
        // stores the result.
        String result = "";
  
        // Converting the given numbers
        // into the string
        String index = String.valueOf(num);
  
        // Traverse the loop from start to end
        for (int i = 0; i < index.length(); i++) {
  
            // Getting the right index
            int k = index.charAt(i) - 48;
  
            // Adding kth position character
            // into the result
            result = result + str.charAt(k);
        }
  
        // Return result
        return result;
    }
  
    // Driver Code
    public static void main(String args[])
    {
  
        // Given String str
        String str = "GeeksforGeeks";
  
        // Given Number num
        int num = 858;
  
        // Printing the result
        System.out.println(generateNewString(str, num));
    }
}


输出
GfG

方法 2:使用一次遍历

  1. 获取字符串和数字以生成新字符串。
  2. 初始化一个存储结果的变量。
  3. 将给定的数字转换为字符串。
  4. 从头到尾遍历循环。
  5. 获取正确的索引并将第 k 个位置字符添加到结果中。
  6. 现在,打印结果。

Java

// Java program to generate String by using
// Strings and numbers
class GFG {
  
    // Function to generate the new String
    public static String generateNewString(String str,
                                           int num)
    {
        // Initialize a variable that
        // stores the result.
        String result = "";
  
        // Converting the given numbers
        // into the string
        String index = String.valueOf(num);
  
        // Traverse the loop from start to end
        for (int i = 0; i < index.length(); i++) {
  
            // Getting the right index
            int k = index.charAt(i) - 48;
  
            // Adding kth position character
            // into the result
            result = result + str.charAt(k);
        }
  
        // Return result
        return result;
    }
  
    // Driver Code
    public static void main(String args[])
    {
  
        // Given String str
        String str = "GeeksforGeeks";
  
        // Given Number num
        int num = 858;
  
        // Printing the result
        System.out.println(generateNewString(str, num));
    }
}
输出
GfG

时间复杂度: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live