📜  TCS编码实践问题|反转字符串

📅  最后修改于: 2021-05-24 17:26:53             🧑  作者: Mango

给定一个字符串,任务是使用命令行参数反转此字符串。

例子:

Input: Geeks
Output: skeeG

Input: GeeksForGeeks
Output: skeeGroFskeeG

方法1:使用另一个字符串存储反向

  • 由于该字符串是作为命令行参数输入的,因此不需要专用的输入行
  • 从命令行参数中提取输入字符串
  • 创建一个String来存储结果反向字符串,例如reversedString
  • 使用循环以相反的顺序逐字符遍历此String字符
  • 现在将每个字符附加到结果的reversedString中
  • 这是必需的反向字符串

程序:

C
// C program to reverse a string
// using command line arguments
  
#include 
#include 
#include 
  
// Function to reverse the String
char* reverseString(char input[])
{
  
    // Get the length of the string
    int length = strlen(input);
    int i;
  
    // String to store the reverse
    char* reversedString
        = (char*)malloc(length * sizeof(char));
  
    // Loop through the string
    // character by character in reverse order
    // and store it into the resultant string
    for (i = length - 1; i >= 0; i--)
        reversedString[length - 1 - i] = input[i];
  
    // Return the reversed String
    return reversedString;
}
  
// Driver code
int main(int argc, char* argv[])
{
    // Check if the length of args array is 1
    if (argc == 1)
        printf("No command line arguments found.\n");
    else {
  
        // Get the command line argument
        // and reverse it
        printf("%s\n", reverseString(argv[1]));
    }
    return 0;
}


Java
// Java program to reverse a string
// using command line arguments
  
class GFG {
  
    // Function to reverse the String
    public static String reverseString(String input)
    {
  
        // String to store the reverse
        String reversedString = "";
  
        // Loop through the string
        // character by character in reverse order
        // and store it into the resultant string
        for (int i = input.length() - 1; i >= 0; i--)
            reversedString += input.charAt(i);
  
        // Return the reversed String
        return reversedString;
    }
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Check if length of args array is
        // greater than 0
        if (args.length > 0) {
  
            // Get the command line argument
            // and reverse it
            System.out.println(reverseString(args[0]));
        }
        else
            System.out.println("No command line "
                               + "arguments found.");
    }
}


C
// C program to reverse a string
// using command line arguments
  
#include 
#include 
#include 
  
// Function to reverse a string
char* reverseString(char str[])
{
  
    int n = strlen(str);
    int i;
    char temp;
  
    // Swap character starting from two
    // corners
    for (i = 0; i < n / 2; i++) {
        temp = str[i];
        str[i] = str[n - i - 1];
        str[n - i - 1] = temp;
    }
  
    return str;
}
  
// Driver code
int main(int argc, char* argv[])
{
    // Check if the length of args array is 1
    if (argc == 1)
        printf("No command line arguments found.\n");
    else {
  
        // Get the command line argument
        // and reverse it
        reverseString(argv[1]);
  
        // Print the reversed string
        printf("%s\n", argv[1]);
    }
    return 0;
}


Java
// Java program to reverse a string
// using command line arguments
  
class GFG {
  
    // Function to reverse the String
    public static String reverseString(String str)
    {
  
        int n = str.length();
        char temp;
  
        // Swap character starting from two
        // corners
        for (int i = 0; i < n / 2; i++) {
            str = str.substring(0, i)
                  + str.charAt(n - i - 1)
                  + str.substring(i + 1, n - i - 1)
                  + str.charAt(i)
                  + str.substring(n - i);
        }
  
        return str;
    }
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Check if length of args array is
        // greater than 0
        if (args.length > 0) {
  
            // Get the command line argument
            // and reverse it
            System.out.println(reverseString(args[0]));
        }
        else
            System.out.println("No command line "
                               + "arguments found.");
    }
}


输出:

  • 在C中
  • 在Java:

方法2:使用其他字符串存储反向字符串

  • 由于该字符串是作为命令行参数输入的,因此不需要专用的输入行
  • 从命令行参数中提取输入字符串
  • 使用循环逐个字符遍历此String字符,直到字符串长度的一半
  • 从一端与另一端的字符互换字符
  • 这是必需的反向字符串

程序:

C

// C program to reverse a string
// using command line arguments
  
#include 
#include 
#include 
  
// Function to reverse a string
char* reverseString(char str[])
{
  
    int n = strlen(str);
    int i;
    char temp;
  
    // Swap character starting from two
    // corners
    for (i = 0; i < n / 2; i++) {
        temp = str[i];
        str[i] = str[n - i - 1];
        str[n - i - 1] = temp;
    }
  
    return str;
}
  
// Driver code
int main(int argc, char* argv[])
{
    // Check if the length of args array is 1
    if (argc == 1)
        printf("No command line arguments found.\n");
    else {
  
        // Get the command line argument
        // and reverse it
        reverseString(argv[1]);
  
        // Print the reversed string
        printf("%s\n", argv[1]);
    }
    return 0;
}

Java

// Java program to reverse a string
// using command line arguments
  
class GFG {
  
    // Function to reverse the String
    public static String reverseString(String str)
    {
  
        int n = str.length();
        char temp;
  
        // Swap character starting from two
        // corners
        for (int i = 0; i < n / 2; i++) {
            str = str.substring(0, i)
                  + str.charAt(n - i - 1)
                  + str.substring(i + 1, n - i - 1)
                  + str.charAt(i)
                  + str.substring(n - i);
        }
  
        return str;
    }
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Check if length of args array is
        // greater than 0
        if (args.length > 0) {
  
            // Get the command line argument
            // and reverse it
            System.out.println(reverseString(args[0]));
        }
        else
            System.out.println("No command line "
                               + "arguments found.");
    }
}

输出:

  • 在C中:
  • 在Java:

想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。