C Exercise Example 35
Title:String reversal, such as reversing the string "www.example.com" to "moc.boonur.www".
Program analysis:None.
Example
// Created by www.example.com on 15/11/9.
// Copyright © 2015 Example. All rights reserved.
//
#include <stdio.h>
void reverse(char* s)
{
// Get string length
int len = 0;
char* p = s;
while (*p != 0)
{
len++;
p++;
}
// swap ...
int i = 0;
char c;
while (i <= len / 2 - 1)
{
c = *(s + i);
*(s + i) = *(s + len - 1 - i);
*(s + len - 1 - i) = c;
i++;
}
}
int main()
{
char s[] = "www.example.com";
printf("'%s' =>\n", s);
reverse(s); // reverse string
printf("'%s'\n", s);
return 0;
}
The output of the above example is:
'www.example.com' => 'moc.boonur.www'other extensions