answersLogoWhite

0

The simplest way is to use the strrev() function.

To manually reverse a string, point to the first and last characters, swap their values, then move the pointers one character towards the middle of the string and repeat. If the pointers point to the same character or pass each other, the string is reversed.

The following is an example implementation. The function only process the given string when the string has 2 or more characters and a null-terminator is found within the first lencharacters.

#include

#include

using namespace std;

void strRev( char * input, size_t len )

{

// Minimum length is three (2 characters, 1 null-terminator).

const int min = 3;

if( len < min )

return;

// Locate the left-most character.

char * l = input;

// Locate the null-terminator:

char * n = l;

while( *n && n < l+len )

++n;

if( *n )

return; // null-terminator not found.

// Confirm the length (ignoring null-terminator).

if( n-l < min-1 )

return;

// Locate the right-most character (left of null-terminator)

char * r = n-1;

// Repeat while right pointer is greater than left pointer.

while( r>l )

{

*n = *l; // use the null-terminator to assist the swap.

*l++ = *r; // change left value and advance pointer.

*r-- = *n; // change right value and retreat pointer.

}

// Restore the null terminator.

*n = 0;

}

int main()

{

// Safest method of entering a string:

string input = "";

printf( "Enter a string: ");

getline( cin, input );

// Determine length of input + null-terminator.

size_t len = input.length() + 1;

// Convert string to a null-terminated string.

char * p = ( char * ) calloc( len, sizeof( char ));

memcpy( p, input.data(), input.length() );

// Reverse the string.

strRev( p, len );

printf( "Reversed: %s\n", p );

// Release memory.

free( p );

return( 0 );

}

User Avatar

Wiki User

13y ago

What else can I help you with?

Related Questions

Do I need to write a program to find a substring in a given string in c plus plus?

No.


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&lt;string&gt; using std::string; string reverse (const string&amp; s) { string str {}; for (auto c : s) str.insert (str.begin(), c); return str; } int main () { std::cout &lt;&lt; "Enter a number: "; string s {}; std::cin &gt;&gt; s; std::cout &lt;&lt; "The number in reverse is: " &lt;&lt; reverse (s); }


Wap in c plus plus to find out the string is palindrome or not in?

use the strrev() function on the given string and compare with original string.If both are equal they are palindromes else not.


How can you get a specific string from large string using c plus plus strings?

std::string::substr();


What is the difference between a C plus plus string and a C-style string?

A std::string is an object that encapsulates an array of type char whereas a C-style string is a primitive array with no members. A std::string is guaranteed to be null-terminated but a C-style string is not.


Write a flowchart of reverse the string in c?

wefwfe


Is there a datatype string in c plus plus?

Yes.


How do you get value of a string in reverse order in c?

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


C plus plus library functions for string operations?

You can use "string" class in C++ for string operations or you may use c style string functions as well. #include &lt;string&gt; String class in C++ provides all basic function to operate on strings. you may details descriptin at http://www.cplusplus.com/reference/string/string/


What is the role of plus operator between two string constant?

The plus operator between string constants allows string concatination: string a = "Hello, "; string b = "World!"; string c = a + b; The output of c would be: "Hello, World!".


Find vowel from given string in C plus plus language?

std::string test ("The cat sat on the mat"); std::string vowels ("aeiouAEIOU"); size_t pos = test.find_first_of (vowels); if (pos != test.npos) std::cout &lt;&lt; "Vowel found at position " &lt;&lt; pos &lt;&lt; std::endl;