📜  将所有大写字符移到末尾的Java程序

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

将所有大写字符移到末尾的Java程序

给定一个包含大写字母和小写字母的字符串。任务是移动字符串末尾的所有大写字符。大写字符的顺序必须与原始字符串中的顺序相同。

Input    : "heLLGFg"
Output   : "hegLLGF"

Input    : "Hello"
Output   : "elloH"

这里我们有两种不同的方法来解决这个问题,如下所示:

  1. 使用字符的ASCII值。
  2. 使用队列数据结构

方法1:使用字符的ASCII值。

  • 逐个迭代字符串的字符,并检查字符串各个字符的 ASCII 值。
  • 对于所有大写字母,ASCII 值位于[65-90]字符值之下。将这些 ASCII 值的字符存储在 String 变量中,并在迭代结束时打印字符串。

例子:

Java
// Java Program to Move All Uppercase Characters to the End
 
// Importing input output classes
import java.io.*;
 
// Mai class
class GFG {
 
    // Method 1
    // To shift uppercase characters
    static void shiftuppercase(String m, int length)
    {
 
        // Taking an empty string
        String temp = "";
 
        for (int i = 0; i < length; ++i) {
 
            // Condition check
            // If the character is uppercase via
            // the ASCII values of the  character
            if (m.charAt(i) >= 65 && m.charAt(i) <= 90) {
                temp += m.charAt(i);
            }
 
            // The character is already lowercase
            else
                System.out.print(m.charAt(i));
        }
 
        // Now, Printing the uppercase string
        System.out.print(temp);
    }
 
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
        // Custom input string
        String m = "heLLGFg";
 
        // Computing the length of the string
        // using length() method
        int length = m.length();
 
        // Calling the method 1 over the custom string taken
        // above to move all uppercase char to the end
        shiftuppercase(m, length);
    }
}


Java
// Java Program to Move All Uppercase Characters to the End
// Using Queue data structures
 
// Importing input output classes
import java.io.*;
// Importing utility classes
import java.util.*;
 
// Main class
class GFG {
 
    // Method 1
    // Main driver method
    static void shiftuppercase(String m, int length)
    {
        // Creating an object of Queue class of character
        // type
        Queue Q = new LinkedList();
 
        // Declaring an initializing to empty string
        String temp = "";
 
        for (int i = 0; i < length; ++i) {
 
            // Condition checkfor the uppercase characters
            // If uppercase use ASCII values of the
            // character
            if (m.charAt(i) >= 65 && m.charAt(i) <= 90) {
                Q.add(m.charAt(i));
            }
 
            // Character is lowercase
            else
 
                // Leave it asities on its index
                System.out.print(m.charAt(i));
        }
 
        // Now, printing the uppercase string till
        // there are elements in queue
        while (Q.size() != 0) {
 
            // Removing all the elements from the queue
            System.out.print(Q.peek());
 
            // Clear the queue
            Q.remove();
        }
    }
 
    // Method 2
    // main driver method
    public static void main(String[] args)
    {
 
        // Given input string
        String m = "heLLGFg";
 
        //  Computing the length of the string
        // using length() method
        int length = m.length();
 
        // Calling the
        shiftuppercase(m, length);
    }
}


输出:

方法二:使用队列数据结构 

  • 队列数据结构实现了先进先出的概念,我们利用这个概念,将大写字符存储在队列中。
  • 除了打印大写字符的所有字符。
  • 迭代后从队列中删除所有元素。

例子:

Java

// Java Program to Move All Uppercase Characters to the End
// Using Queue data structures
 
// Importing input output classes
import java.io.*;
// Importing utility classes
import java.util.*;
 
// Main class
class GFG {
 
    // Method 1
    // Main driver method
    static void shiftuppercase(String m, int length)
    {
        // Creating an object of Queue class of character
        // type
        Queue Q = new LinkedList();
 
        // Declaring an initializing to empty string
        String temp = "";
 
        for (int i = 0; i < length; ++i) {
 
            // Condition checkfor the uppercase characters
            // If uppercase use ASCII values of the
            // character
            if (m.charAt(i) >= 65 && m.charAt(i) <= 90) {
                Q.add(m.charAt(i));
            }
 
            // Character is lowercase
            else
 
                // Leave it asities on its index
                System.out.print(m.charAt(i));
        }
 
        // Now, printing the uppercase string till
        // there are elements in queue
        while (Q.size() != 0) {
 
            // Removing all the elements from the queue
            System.out.print(Q.peek());
 
            // Clear the queue
            Q.remove();
        }
    }
 
    // Method 2
    // main driver method
    public static void main(String[] args)
    {
 
        // Given input string
        String m = "heLLGFg";
 
        //  Computing the length of the string
        // using length() method
        int length = m.length();
 
        // Calling the
        shiftuppercase(m, length);
    }
}

输出: