📜  TCS 编码练习题 |反转字符串

📅  最后修改于: 2021-10-23 07:56:49             🧑  作者: Mango

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

例子:

Input: Geeks
Output: skeeG

Input: GeeksForGeeks
Output: skeeGroFskeeG

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

  • 由于字符串是作为命令行参数输入的,因此不需要专用的输入行
  • 从命令行参数中提取输入字符串
  • 创建一个字符串来存储结果反转字符串,比如 reversedString
  • 通过使用循环字符此字符串字符横动,以相反的顺序
  • 现在将每个字符附加到结果 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 基础课程