string reverse

Pages: 12
Sep 28, 2014 at 3:33am
The question is to reverse the string using 2 different functions separately ,
I am not able to get to the logic of the second function :


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
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
using namespace  std;

//1st function
char* stringreverse(const char* src, char* reversed){
    
    char* temp =  reversed;
    while(*src++);
    src--;
    while(*reversed++ = *--src);
    return temp;
    
    
}

//2nd function
char* stringreverse2(char* src){
    
      //what should i do ?

    
}



int main()
{
    
    char string[] = "abcdefghij";
    char copiedstr[sizeof(string)];
    
    char* src  = string;
    char* reversed  = copiedstr;
    
    cout<<stringreverse(src, reversed)<<endl;
    cout<<stringreverse2(src); // i am not able to define this function

}
Sep 28, 2014 at 7:35am
any help?
Sep 28, 2014 at 8:28am
Have 2 pointers. One pointing to the beginning of the string, one to the last character. Swap values those pointers are pointing to. Icrement first pointer, decrement second. Repeat until pointers meet.
Sep 28, 2014 at 10:17am
closed account (SECMoG1T)
You can use the string reverse iterator to copy characters
Sep 28, 2014 at 2:35pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//Reverse C-string using single argument
char* Reverse(char *src)
{
    char *q = src ;    
    while(*q++) ; // reach to the end of array
    q -=2 ;	//now *q == last element
    
    char *p = new char[sizeof(char) * (q - src + 2)] ; 
    char *r = p ; // keep track of p's current address.

    for( ; q >= src; p++, q--)
        *p =*q;        
    *p = '\0' ;
    
    return r ; //main src unchanged, as *p != src;
}
Last edited on Sep 28, 2014 at 2:52pm
Oct 1, 2014 at 6:16am
the above code would leak memory right …cause after new i am not doing delete……what would be a c -style solution to this leak ?
Oct 1, 2014 at 10:46am
??
Oct 1, 2014 at 12:01pm
sorry! the leak was incautious.
so lets do without dynamic memory >
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cstring> // strlen()
#include <algorithm> //swap()
using namespace std;

//Reverse C-string using single argument
char* Reverse(char *src) {	
int i=0;
int len = strlen (src)-1;
while(i<len)
   swap (src[i++],src[len--]);    
return src ;
}

int main() {
char ch[]="hello world!";
char *pt = ch;
pt = Reverse (pt);
cout << pt;
return 0;
}
Oct 1, 2014 at 1:30pm
well, code of 8:35am, does not lead to memory leak if you receive it by a pointer, and delete later >
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
27
#include <iostream>
//#include <cstring> // strlen()
//#include <algorithm> //swap()
using namespace std;

//Reverse C-string using single argument
char* reverse(char *src)
{   char *q = src ;    
    while(*q++) ; // reach to the end of array
    q -=2 ;	//now *q == last element    
    char *p = new char[sizeof(char) * (q - src + 2)] ; 
    char *r = p ; // keep track of p's current address.
    for( ; q >= src; p++, q--)
        *p =*q;        
    *p = '\0' ;   
    //cant delete[] p; r will be lost
    return r ; //main src unchanged, as *p != src;
}

int main() {
char ch[]="hello world!";
char *pt = ch;
pt = reverse (pt);
cout << pt;
delete[] pt; // here
return 0;
}

Last edited on Oct 1, 2014 at 1:35pm
Oct 1, 2014 at 1:43pm
its better Reverse function of 6:01pm be void, as it changes the caller:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

void Reverse(char *src) {
int i=0;
int len = strlen (src)-1;
while(i<len)
   swap (src[i++],src[len--]);    
return;
}

int main() {
char ch[]="hello world!";
char *pt = ch;
Reverse (pt); // ch[] changes
cout << ch;
return 0;
}
Oct 1, 2014 at 2:59pm
Proper C logic would be to reverse string in-place (modifying original) and return it. That is how C standard library works. Almost no standard functions allocate theitr own memory, they work only with buffers provided by user.

Additionally function signature suggest that passed string would be modified.
Oct 1, 2014 at 5:24pm
std::string is much easier..
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <algorithm> // reverse();

int main()
{  	
   string s = "Hello world!";
   std::reverse( s.begin(), s.end() );
   std::cout << s;  
   return 0;
}
Oct 1, 2014 at 5:30pm
std::string is much easier..
You forgot to #include <string>

Also:
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <algorithm> // reverse();

int main()
{  	
   char s[] = "Hello world!";
   std::reverse( std::begin(s), std::end(s) - 1);
   std::cout << s;  
}
Last edited on Oct 1, 2014 at 5:31pm
Oct 1, 2014 at 7:40pm
as well as your code runs without <cstring>
My code does not use anything declared in cstring header. Your code uses type declared in string header.

my code runs
Not an argument. It is just an implementation detail that iostream happens to include string. Some other compiler might not do that. Standard says that you need to explicitely include all headers which contain entities you are using. (My code is not perfect either: i need to include iterator)

http://stackoverflow.com/questions/9539650/why-does-omission-of-include-string-only-sometimes-cause-compilation-failur
Oct 1, 2014 at 8:33pm
its not serious error! just a matter of forgetting. if compile, anyone can get it.
however, my main mistake was to forget "using namespace std;"
the code below runs both in devcpp and http://cpp.sh/
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <algorithm> // reverse();
//#include <string>
using namespace std;

int main()
{  	
   string s = "Hello world!";
   reverse( s.begin(), s.end() );
   cout << s;  
   return 0;
}

but the following does not.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <algorithm> // reverse();
#include <string>
//using namespace std;

int main()
{  	
   string s = "Hello world!";
   reverse( s.begin(), s.end() );
   cout << s;  
   return 0;
}


these doesn't matter at all, because all focus would be in line 7, both of mine and yours ;)
Oct 2, 2014 at 12:56am
@annup30, you should use std:: to scope to the things in the namespace. Otherwise it makes the namespaces completely useless. They are meant to avoid naming conflicts. When you throw everything in the global namespace it does the opposite. Anyways..If you are going to use std::strings why not do something like
1
2
std::string str1 = "Hello, world!";
std::string str2(str1.rbegin(), str1.rend());
or
1
2
std::string str = "Hello, world!";
str = std::string(str.rbegin(), str.rend());
Though, these won't even help the OP since he seems to be doing it with dynamic c-strings.
Last edited on Oct 2, 2014 at 12:58am
Oct 2, 2014 at 9:35am
Otherwise it makes the namespaces completely useless

i was watching code of some best programmers of the world - the Google Code Jam finalist's solutions to extreme problems - nearly two third of the solutions were in C++. everyone used "using namespace std;"

problems: https://code.google.com/codejam/contest/7214486/dashboard

solutions: https://code.google.com/codejam/contest/7214486/scoreboard#vf=1
sol/statistics: http://www.go-hero.net/jam/13/solutions
Oct 2, 2014 at 9:38am
Code Jam
Not the best reference. He (a) needs to write code extremely fast (which, mind you, is very rarely the case in real life), (b) a good programmer and know exactly rules of name lookup and which names are contained in std namespace. Can you name at least 20 names which are not commonly used but exist in std namespace?
Oct 2, 2014 at 10:17am
commonly used

is a vague term. such as all contestants included <vector> (//std::vector)
but if you use vector in these beginners forum, OPs will normally say that they don't know vector.
so, for teaching you have to mention "std::x" explicitly.
but for elements you already know, if written in every instance - its boring. without it i have clearer visualization of code, and bug could be found quicker.
not to mention, i also like smaller and easier code.
in terms of efficiency, does your program run faster with every instance "std::x" ?
Oct 2, 2014 at 10:38am
in terms of efficiency, does your program run faster with every instance "std::x" ?
No, it should not, it is compile time thing.

Find the potential problems and try to guess why this wasn't a problem earlier:
1)
1
2
using namespace boost;
using namespace std;

2)
1
2
#include <geometryV1.6.h>
using namespace std;

3)
1
2
using namespace geom;
using namespace std;


http://stackoverflow.com/questions/2712076/how-to-use-an-iterator
http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice
Pages: 12