Permutation of String in c++

•March 1, 2010 • Leave a Comment

Hello people again , one day I was thinking calculating all permutations of string in c++ is difficult
but now when I got familiar with recursion that was so easy to do the job.
I had seen many codes on internet but didn’t get anyone. Here is my code. It’s self explanatory.

#include <cstdio>

#include <iostream>

#include <string>

#include <vector>

using namespace std;

vector<string> permute(string ,int );

main()

{

string s=”1234″;

vector<string> output=permute(s,s.size());

for(int i=0;i<output.size();i++)

cout<<output[i]<<endl;

}

vector<string> permute(string s,int n) //this will return all permutions of string s as array of strings

{

if(n==1)

{

vector<string> v;

v.push_back(s);

return v;

}

else

{

vector<string> v;

for(int i=0;i<s.size();i++)

{

string temp=””;

for(int j=0;j<s.size();j++)

if(j!=i) temp+=s[j]; // temp is string of all charactor in s except s[i]

vector<string> vv;

vv = permute(temp,n-1); // logic is keep s[i] to first place and concatinate it with all permution of temp

for(int k=0;k<vv.size();k++)

v.push_back(s[i]+vv[k]);

}

return v;

}

}

Important facts in c

•March 1, 2010 • Leave a Comment

1.  character \b is used as back space.
2. character \r is used to print from the beginning of the line.
3. pre-increment with post-increment like ++i++ will give compile error as non-lvalue increment
4. Expression evaluation from left to right and argument passing from right to left. // VIMportant

Little Ignorence may fail your code

•August 5, 2009 • Leave a Comment

Only one codition made my problem in topcoder’s 250 point pronlem in div 2 failed. You can view my code here http://www.topcoder.com/stat?c=problem_solution&rm=301792&rd=13898&pm=10489&cr=22692505. Can you tell what’s the value of  s.size()-2 where s is the c++ string of value 1. Many of people will answer -1 to this but actually it will give some big interger with negative value. To get value of 1 we need to typecast s.size() to integer becouse s.size assumes its value to be unsigned integer. So while writing your code sometimes these things may lead to wrong answer.

In this srm 444, I challenged one guy successfully but mine code failed. So I learned two things from this one is “We dont see our faults but we see find fault in others” and other is “We should learn from our own mistakes”.

Which compiler should I use for C or C++?

•March 25, 2009 • Leave a Comment

This is my first post in my blog . As my career is related with computers/software,

I would like to tell you which compiler one should  use for different purposes.

First thing is that if you are beginner (i.e. you have just learn to write “Hello World”)

then my suggestion is turbo C is more preferable.If you use vista then use turbo-c 4.5

as other versions of turbo C will not work in vista and for xp users they can try any version of turbo-c.

Once you are done with Hello World then you must try Dev-Cpp compiler/IDE  as

It uses the MinGW port of the GCC (GNU Compiler Collection) as its compiler

and debugging is very easy, so one can easily find his bugs in program.

If you use  linux then you can use GCC/G++.

My suggestion will be don’t use gcc in linux as it’s not easy to debug your code

using gnome debugger.

You can download Dev-Cpp here. If you find some trouble with dev-cpp then

do some google you may find a solution or else post your comment here

I can help you at my best.

Further if you want to make a project then Dev-cpp can be used but my suggestion

will be to use visual c++ or net-beans.

Check out some coding sites like SPOJ or TOPCODER they will improve your coding

skills. For SPOJ you should use Dec-cpp not turbo C.

Thank you for your time for reading my blog.