answersLogoWhite

0

Assume C#, not C:

Traditional way:

public string Reverse(string s)

{

if (string.IsNullOrEmpty(s)) return s; // "" or null

char[] characters = s.ToCharArray();

Array.Reverse(characters);

return new string(characters);

}

or as an extension method:

public static string Reverse(this string s) {

if (s == "") return "";

char[] characters = s.ToCharArray();

Array.Reverse(characters);

return new string(characters);

}

The differences of the 2 methods above is on the caller (how to use Reverse()), and they may co-exist:

For example:

string test = "abc";

string result1 = Reverse(test); // traditional way

string result2 = test.Reverse(); // call the extension

User Avatar

Wiki User

13y ago

What else can I help you with?

Continue Learning about Engineering

What is the use of Reverse String in C program?

The use of the reverse string in C program is used to reverse the letters in the string. An example would be reverse me would be reversed to em esrever.


How do you convert a number that consist of alphabet and number in reverse order?

To reverse a number, first convert the number to a string, then reverse the string. Given your number consists of alphanumeric characters, the number must already be a string so simply reverse the string: #include<string> using std::string; string reverse (const string& s) { string str {}; for (auto c : s) str.insert (str.begin(), c); return str; } int main () { std::cout << "Enter a number: "; string s {}; std::cin >> s; std::cout << "The number in reverse is: " << reverse (s); }


Write a flowchart of reverse the string in c?

wefwfe


Print reverse string in c sharp?

To print a reverse string in C#, you can use the Array.Reverse method or LINQ. Here's a simple example using Array.Reverse: string original = "Hello, World!"; char[] charArray = original.ToCharArray(); Array.Reverse(charArray); string reversed = new string(charArray); Console.WriteLine(reversed); This code converts the string to a character array, reverses the array, and then creates a new string from the reversed array before printing it.


What is the program to create a string class which stores a string value in c plus plus?

C++ already provides a string class in the C++ standard template library. #include<iostream> #include<string> int main() { using namespace std; string s {"Hello world!"}; cout << s << endl; }

Related Questions

What is the use of Reverse String in C program?

The use of the reverse string in C program is used to reverse the letters in the string. An example would be reverse me would be reversed to em esrever.


How do you convert a number that consist of alphabet and number in reverse order?

To reverse a number, first convert the number to a string, then reverse the string. Given your number consists of alphanumeric characters, the number must already be a string so simply reverse the string: #include<string> using std::string; string reverse (const string& s) { string str {}; for (auto c : s) str.insert (str.begin(), c); return str; } int main () { std::cout << "Enter a number: "; string s {}; std::cin >> s; std::cout << "The number in reverse is: " << reverse (s); }


Write a flowchart of reverse the string in c?

wefwfe


Print reverse string in c sharp?

To print a reverse string in C#, you can use the Array.Reverse method or LINQ. Here's a simple example using Array.Reverse: string original = "Hello, World!"; char[] charArray = original.ToCharArray(); Array.Reverse(charArray); string reversed = new string(charArray); Console.WriteLine(reversed); This code converts the string to a character array, reverses the array, and then creates a new string from the reversed array before printing it.


How to Program for reversal of string ven123kat in c?

#include #include #include int reverse(int i);char st[]="ven123kat";void main() {printf("\nThe string is: %s", st);reverse(0);printf("\nReversed string is: %s", st);getch();}int reverse(int i) {if (i


What is value in C?

A synonym for literal: 12, -1.3, "string", 'a'


What is the program to create a string class which stores a string value in c plus plus?

C++ already provides a string class in the C++ standard template library. #include<iostream> #include<string> int main() { using namespace std; string s {"Hello world!"}; cout << s << endl; }


How do you tune a 6 string ukulele?

It will have the basic G, C, E, A tuning, with the two additional strings. These are paired with the C and A strings. The second "A" string is tuned to the same note as the other "A" string. The additional "C" string is going to be either an octave up or down. The strings in order will be G, C, C', E, A, A.


Write a C program to accept a string from user and display its ascii value and then display sum of all ascii value of strings?

//C program to accept a string from user and //display its ascii value and //then display sum of all ascii value of strings #include<stdio.h> #include <string.h> int main() { char String[100]; int Sum,Index; Sum=0; //Sum is initially zero printf("Enter the string:\n"); gets(String); //Accept String from User for(Index=0;Index<strlen(String);Index++) { Sum+=(String[Index]); //Adds (the ASCII values of) the String characters. } printf("The sum is %d\n",Sum); //Printing it as %d gives the equivalent ASCII value. return 0; }


What is the syntax for string in c?

The C programming language has no notion of a string. The C runtime libraries interpret a string as an array of 'char' (sometimes 'unsigned char'), where a byte (char) with numerical value zero (often written as '\0' in C) denotes the end of the string. Modern variations also support modern forms of strings based on different data types (wchar, etc) in order to support more complex encodings such as Unicode. These, too, are interpretations of combinations of language features, but not a built-in part of the language.


If String str US then what is the value of str.substring(2 4)?

In C++, the return value will be an empty string because 2 is equal to the string length (the second parameter, 4, is simply ignored). If the first parameter were greater than the string length, an out_of_range exception will be thrown instead.


A program of reverse a number using recursion in C?

(in C#)string Reverse (string input) {There are two cases; the recursive case and the trivial case.The trivial case is when the string has zero or one character. In this case, the reversal of the string is the same.if (input.Length