Function prototype in C++
#include <iostream>
#include <cstring>
using namespace std;
void reverse(char *str, int count = 0);
int main()
{
char *s1 = "This is a test.";
char *s2 = "I like C++.";
reverse(s1); // reverse entire string
reverse(s2, 7); // reverse 1st 7 chars
cout << s1 << '\n';
cout << s2 << '\n';
return 0;
}
void reverse(char *str, int count)
{
int i, j;
char temp;
if(!count) count = strlen(str)-1;
for(i = 0, j=count; i <j; i++, j--) {
temp = str[ i ];
str[ i ] = str[j];
str[j] = temp;
}
}
Related examples in the same category