Different Ways to Generate String by using Characters and Numbers in Java

Given a number num and String str, the task is to generate the new String by extracting the character from the string by using the index value of numbers.

Examples:

Input: str = “GeeksforGeeks”

           num = 858

Output: GfG



Explanation: The 8th, 5th, and 8th position of the characters are extracting from the given string and generate a new string.

Input: str = “Java”

           num = 12

Output: The 1st and 2nd position of the characters are extracting from the given string and generate a new string.
 

Method 1: Using Two Traversals

  1. Get the string and number to generate a new string.
  2. Initialize a variable that stores the final answer.
  3. Converting the given number into the string.
  4. Traverse the loop from start to end.
  5. Getting the last digit of the number and add kth position character into the variable answer.
  6. Traverse the loop in the reverse order and adding the jth position character into the variable finalAnswer.
  7. Now, print the result.

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


Output

GfG

Method 2: Using One Traversal

  1. Get the string and number to generate a new string.
  2. Initialize a variable that stores the result.
  3. Converting the given number into the string.
  4. Traverse the loop from start to end.
  5. Get the right index and adding kth position character into the result.
  6. Now, print the result.

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


Output

GfG

Time Complexity: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.