Posts

Showing posts from July, 2016

Printing all the permutation of the string using recursion

           dog    dog ,  odg ,  god    (in first call 1st character is swapped with all postions (1st to last)) dgo     ogd       gdo   (in 2nd call ,2nd character is swapped with all postions(2nd to last) ) #include<bits/stdc++.h> using namespace std; void permu(string k,int fst,int lst) {     if(fst==lst)         cout<<k<<endl;     for(int i=fst;i<=lst;i++)     {         swap(k[fst],k[i]);         permu(k,fst+1,lst);         swap(k[fst],k[i]);     } } int main() {     string s;     cin>>s;     int lst=s.size()-1;     permu(s,0,lst);     return 0; }

Reverse String using Recursion C++

Here the Logic is simple to print the last character of the string for each function call and again call the reverse function again ,this time the string doesn't contain last character. #include<bits/stdc++.h> using namespace std; void reve(string k) {   int sz=k.size();   if(sz==1)     cout<<k[sz-1];   else   {      cout<<k[sz-1];      string p=k.substr(0,sz-1);      reve(p);   } } int main() {     string s;     cin>>s;     reve(s);     return 0; }

LeadSquared Interview Questions

On 12th july , I got a chance to sit for the written round of Leadsquared drive. In 1 hour we have to do 3 java questions. 1st question->Excel sheet problem                       ex. 1 -> A , 2->B , 26->Z ,27->AA   import java.util.*; public class excel { public static void main(String args[]) {   Scanner s=new Scanner(System.in);        int r,n=s.nextInt();   String k=" ";        while(n>0)   { r=n%26;           if(r==0)  { String ch="Z";             k=k+ch; n=n/26 -1;  }           else  {  int ch=r-1+(int)'A';  char p=(char)ch;  String h= Character.toString(p);               k=k.concat(h);          n=n/26; ...

c++ Program to reverse the sentence

C++ Program to reverse the sentence Input- "My name is Nikhil" Output "Nikhil is name My" Here the idea to reverse each word in 1st phase and in 2nd phase reverse the whole sentence. #include<bits/stdc++.h> using namespace std; int main() {     string s;     getline(cin,s);     cout<<s<<endl;     int l=s.size();     int pre=0;     for(int i=0;i<l;i++)     {       if(s[i]==' ')       {         int k=i-1;         while(pre<k){char c=s[pre]; s[pre]=s[k]; s[k]=c; pre++; k--; }         pre=i+1;       }       else if(i==l-1)       {         int k=i;         while(pre<k){char c=s[pre]; s[pre]=s[k]; s[k]=c; pre++; k--; }       }     }     int bck=0; ...