#include <iostream>
usingnamespace std;
//1st function
char* stringreverse(constchar* 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
}
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.
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.
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)
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/
@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
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;"
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?
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" ?