How to reverse a C style string completely [closed]
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this questionI'd like to reverse a C style string (null-terminated) completely, i.e. including the null character.
The null character should be the first element in the reversed string.
That's actually pretty easy:
char *reversed = "";
:)
A C string starting with a '\0'
is an empty string. And unless you do things (like adding +1) with the pointer or use functions which expect memory (pointer+size) instead of strings it doesn't matter what comes behind the '\0'
If you actually needed the real string to be present in memory after the '\0'
: Take the function which writes your reversed string to a given memory location (void strreverse(char *dest, const char *string)
).
Then call it like that: char dest[ENOUGH_SPACE]; dest[0] = '\0'; strreverse(dest + 1, YOUR_STRING);
Reversing a string is actually a good task to give to a beginner, if you haven't done any programming it would require you to sit down with a piece of paper and think about how characters are stored and how you would swap them around. Also, it requires you to be careful with when you stop so that it works both with strings with an even and odd number of characters.
Hence, why don't you give it a try yourself?
However, I don't really see the point in including the null terminator in the reverse, it would only make the string look empty when used with standard string functions. Also, I hope this is not a little-endian/big-endian mixup, as endianess don't apply to strings.
void memcpy_reverse (char* dest, const char* source, size_t n)
{
size_t i;
for(i=0; i<n; i++)
{
dest[i] = source[n-i-1];
}
}
Here's another way.We take a pointer and make it point to the target.Then put "x" at the location pointed by the pointer.Reverse your string and then concat it with the target.Now assign '\0' to the location pointed by pointer.
WCHAR * wszStr = "Hello World";
wcscpy_s(wszTarget,256,L"x");
WCHAR *pTempNULL = wszTarget;
wcscat_s(wszTarget,256,wcsrev(wszStr)/*Or Reverse you string with your algorithm*/);
*pTempNULL = '\0';
Take the String. Store it in char[] array. Use 2 indexes to loop through the characters and swap them. Reverse it back to String.
I guess we can implement this using something like this in Java:
public class ReverseString
{
public static void main(String[] args)
{
String str = "a ";
char[] myChar = str.toCharArray();
System.out.println(str);
int p1 = 0;
int p2 = myChar.length -1;
while(p1<p2)
{
char temp = myChar[p1];
myChar[p1]= myChar[p2];
myChar[p2] = temp;
p1++;
p2--;
}
str = str.copyValueOf(myChar);
System.out.println(str);
}
}
public static void main(String[] args) {
String str = Generator.getSaltString();
str += '\0';
System.out.println("C style string to reverse: " + str + "****");
reverse(str);
}
private static String reverse(String str) {
int length = str.length();
char[] chars = new char[length];
int j = 0;
for (int i = length - 1; i >= 0; i--) {
chars[j++] = str.charAt(i);
}
String resultStr = new String(chars);
System.out.println("****" + resultStr + "********");
return resultStr;
}
Check this C# code. You can see this code in action here
public static string ReverseCString(string input)
{
char[] charArray = input.ToCharArray();
Array.Reverse(charArray);
return new string (charArray);
}
精彩评论