Dec 26, 2010 at 1:57am UTC
How do you change a letter of a string to another and how do you invert the letters of a string?
Exemple:
Francisco -> FrXncXscX
Francisco -> ocsicnarF
Dec 26, 2010 at 2:23am UTC
To change a letter in a string:
1 2 3 4 5 6 7
char * str="The bird called to its mate" ;
//change the letters
str[4]='g' ;
str[5]='o' ;
str[6]='a' ;
str[7]='t' ;
cout<<str;
The goat called to its mate
To invert a string, you would need to use the concept above in a function:
1 2 3 4 5 6 7 8
char * reverse(char * str){
unsigned size=strlen(str);
unsigned x;
for (x=0;x<size;++x,--size){
str[x]^=str[size];
str[size]^=str[x];
str[x]^=str[size];}
return str;}
Last edited on Dec 26, 2010 at 2:30am UTC
Dec 26, 2010 at 2:49am UTC
Isn't there a string function for this problem? I'm doing the exercise of the forum 'Strings are your friends, until they betray you'. The question says that strings and string functions are requirable.
Dec 26, 2010 at 2:57am UTC
I would use find() and replace() if you want to be generic, and reverse iterators (rbegin(), rend()) to go backwards.
Dec 26, 2010 at 4:33am UTC
so you don't need an extra variable.
And I think that the xor operation is shorter and faster than move.
If it is encapsulated, who cares? You will need to provide the ^= operator for user types (that doesn't make sense).
you could create a replace_if with transform.
or just loop over your string with find_first_of
Last edited on Dec 26, 2010 at 4:43am UTC
Dec 26, 2010 at 5:22am UTC
Well, with the variable it can be optimized to a XCHG instruction with a register. Yours is too un-obvious for that.
Last edited on Dec 26, 2010 at 5:25am UTC
Dec 26, 2010 at 5:41am UTC
Ok. I've gprof'd it and I surrender.
given this code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#include <string.h>
char * weirdstrrev(char * str){
unsigned size=strlen(str);
unsigned x;
for (x=0;x<size;++x,--size){
str[x]^=str[size]; //NOOO!!
str[size]^=str[x]; //WHYY??
str[x]^=str[size];}//AAAA!!
return str;}
char * normalstrrev(char * str){
unsigned size=strlen(str);
unsigned x;
for (x=0;x<size;++x,--size){char c=str[x];str[x]=str[size];str[size]=c;}
return str;
}
int main(void )
{
char x[30]="this is a test of the swap." ;
register int i=100000000;
while (i--)weirdstrrev(x);
i=100000000;
while (i--)normalstrrev(x);
}
with O2 optimization,
gprof shows your code is 7% faster:
Each sample counts as 0.01 seconds.
% cumulative self self total
time seconds seconds calls ns/call ns/call name
39.34 0.72 0.72 100000000 7.20 7.20 normalstrrev
36.61 1.39 0.67 100000000 6.70 6.70 weirdstrrev
24.04 1.83 0.44 main
-----------------------------------------------
Last edited on Dec 26, 2010 at 5:55am UTC
Dec 26, 2010 at 8:24am UTC
seems familiar:
http://www.cplusplus.com/forum/beginner/33124/#msg178184
Dunno if there is a "simpler" way, but your code will be cleaner if you create a function.
1 2 3 4
void subs( std::string &, const std::string &, char );
subs( firstname, "aeiou" , 'z' );
subs( lastname, "aeiou" , 'z' );
Last edited on Dec 26, 2010 at 8:26am UTC