Code For Alls..!

get the code!!

Showing posts with label cpp. Show all posts
Showing posts with label cpp. Show all posts

Monday 18 September 2017

                                                                          Three Strings
Three Strings


Given N string values, the program must print 3 string values as the output as described in the Example Input/Output section.
Input format:
The first line will contain N denoting the number of string values.
Next N lines will contain the N string values.
Output format:
Three lines containing string values as described in the Example Input/Output section.
Example Input/Output 1:
Input:
3
JOHN
JOHNY
JANARDHAN
Output:
JJOJAN
OHHARD
NNYHAN
Example Input/Output 2:
Input:
4
JOHN
JOHNY
JANARDHAN
MIKESPENCER
Output:
JJOJANMIKE
OHHARDSPE
NNYHANNCER
C++ Program:
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
string s,s1="",s2="",s3="";
int n;
cin>>n;
for(int i=0;i<n;i++)
{
    cin>>s;
    s1+=s.substr(0,i+1);
    s2+=s.substr(i+1,s.length()-2*(i+1));
    s3+=s.substr(s.length()-(i+1));
}
cout<<s1<<"\n"<<s2<<"\n"<<s3;
}

Sunday 17 September 2017

                                                Minimum Distance Between Alphabets (Id-3109)
Minimum Distance Between Alphabets (Id-3109)


Given a string S and two alphabets C1 and C2 present in S, find the minimum distance D  between C1 and C2 in S.
Input Format:
The first line will contain S.
The second line will contain C1 and C2 separated by a space.
Output Format:
The first line will contain D.
Boundary Conditions:
2 <= Length of S <= 100
Example Input/Output 1:
Input:
spaceship
c s
Output:
1
c++ Program:
#include <iostream>

using namespace std;

int main(int argc, char** argv)
{
string s;
cin>>s;
char a,b;
cin>>a;
cin>>b;
int m=s.length(),flag=0,d=0;
for(int i=0;i<s.length();i++)
{
    if(s[i]==a||s[i]==b)
    {
        if(flag==1&&d!=0)
        {
        if(d<m)
        m=d;
        d=0;
        }
        else
        flag=1;
    }
    if(flag==1)
    d++;
}
cout<<m-1;
}


Sunday 10 September 2017

                                               Isolate Interlaced Strings (Id-3077) 



Two string values S1 and S2 are interlaced and passed as a single input string S. Given L1 which is the length of S1, print S1 and S2 as the output.
Input Format:
The first line contains S.
The second line contains L1 and L2 separated by a space.
Output Format:
The first line contains S1.
The second line contains S2.
Boundary Conditions:
4 <= LENGTH(S) <= 100
1 <= LENGTH(S1) <= 99
1 <= LENGTH(S2) <= 99
Example Input/Output 1:
Input:
LBARZIYSK
4
Output:
LAZY
BRISK

c++ program:

#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
string s,f="";
cin>>s;
int n,i;
cin>>n;
for(i=0;i<s.length();i=i+2)
{
    if(f.length()<n)
    {
    f+=s[i];
    s[i]='0';
    }
    if(s.length()-i==n-i/2)
    break;
}
if(i<s.length())
while(i<s.length())
{f+=s[++i];n
s[i]='0';
}
cout<<f<<"\n";
 i=0;
 while(i<s.length())
 {
     if(s[i]!='0')
 cout<<s[i];
 i++;
 }

}

Thursday 7 September 2017

                                                                String Zig Zag Pattern

Given a string S and an integer N as the input, the program must split the string into groups whose size is N and print them as the output in separate lines in a zig zag manner. If the last group size is less than N then the remaining letters must be filled with asterisk as shown in the Example Input/Output.
Input Format:
The first line contains S.
The second line contains N.
Output Format:
LENGTH(S)/N + LENGTH(S)%N lines containing the desired pattern.
Boundary Conditions:
4 <= LENGTH(S) <= 500
2 <= N <= LENGTH(S)
Example Input/Output 1:
Input:
ENVIRONMENT
3
Output:
ENV
ORI
NME
*TN
Example Input/Output 2:
Input:
ENVIRONMENT
4
Output:
ENVI
MNOR
ENT*
Example Input/Output 3:
Input:
EVERYDAY
2
Output:
EV
RE
YD
YA
C++program:
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
string s;
int n,c=0,l;
cin>>s;
cin>>n;
l=s.length();
while(l%n!=0)
{
s.append("*");
l=s.length();
}
int i=0,t=0;
while(i<l)
    {
    cout<<s[i];
    c++;
    if(c==n)
    {
    c=0;
    cout<<"\n";
    i=i+n;
    t++;
    }
    else
    {
    if(t%2==0)
    i++;
    else
    i--;
    }
}
}

Sunday 3 September 2017


You’re given the pointer to the head node of a linked list. Change the next pointers of the nodes so that their order is reversed. The head pointer given may be null meaning that the initial list is empty.
Input Format
You have to complete the Node* Reverse(Node* head) method which takes one argument - the head of the linked list. You should NOT read any input from stdin/console.
Output Format
Change the next pointers of the nodes that their order is reversed and return the head of the reversed linked list. Do NOT print anything to stdout/console.
Sample Input
NULL
2 --> 3 --> NULL
Sample Output
NULL
3 --> 2 --> NULL
Explanation
1. Empty list remains empty
2. List is reversed from 2,3 to 3,2
c++ program:
/*
  Reverse a linked list and return pointer to the head
  The input list will have at least one element  
  Node is defined as 
  struct Node
  {
     int data;
     struct Node *next;
  }
Reverse(Node  head)
        prev=NULL       //Previous Pointer
        cur=head        //Current Node
        nxt=*cur.next   //Next Pointer
        while cur is not NULL
            nxt=(*cur).next
            (*cur).next=prev
            prev=cur
            cur=nxt
        head=prev
        return head
*/
Node* Reverse(Node *head)
{
 struct Node *prev=NULL,*cur,*nxt;
    cur=head;
    nxt=cur->next;
    while(cur!=NULL)
        {
        nxt=cur->next;
    cur->next=prev;
    prev=cur;
    cur=nxt;
    }
    head=prev;
    return(head);
    // Complete this method
}




You are given the pointer to the head node of a linked list and you need to print all its elements in reverse order from tail to head, one element per line. The head pointer may be null meaning that the list is empty - in that case, do not print anything!
Input Format
You have to complete the void ReversePrint(Node* head) method which takes one argument - the head of the linked list. You should NOT read any input from stdin/console.
Output Format
Print the elements of the linked list in reverse order to stdout/console (using printf or cout) , one per line.
Sample Input
1 --> 2 --> NULL
2 --> 1 --> 4 --> 5 --> NULL
Sample Output
2
1
5
4
1
2
Explanation
1. First list is printed from tail to head hence 2,1
2. Similarly second list is also printed from tail to head.

c++ program:
/*
  Print elements of a linked list in reverse order as standard output
  head pointer could be NULL as well for empty list
  Node is defined as 
  struct Node
  {
     int data;
     struct Node *next;
  }
*/
void ReversePrint(Node *head)
{
  if(head!=NULL)
      {
      ReversePrint(head->next);
  cout<<head->data<<"\n";
  }
    // This is a "method-only" submission. 
  // You only need to complete this method. 
}




You’re given the pointer to the head node of a linked list and the position of a node to delete. Delete the node at the given position and return the head node. A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The list may become empty after you delete the node.
Input Format
You have to complete the Node* Delete(Node* head, int position) method which takes two arguments - the head of the linked list and the position of the node to delete. You should NOT read any input from stdin/console. position will always be at least 0 and less than the number of the elements in the list.
Output Format
Delete the node at the given position and return the head of the updated linked list. Do NOT print anything to stdout/console.
Sample Input
1 --> 2 --> 3 --> NULL, position = 0
1 --> NULL , position = 0
Sample Output
2 --> 3 --> NULL
NULL
Explanation
1. 0th position is removed, 1 is deleted from the list.
2. Again 0th position is deleted and we are left with empty list.

C++program:
/*
  Delete Node at a given position in a linked list 
  Node is defined as 
  struct Node
  {
     int data;
     struct Node *next;
  }
*/
Node* Delete(Node *head, int position)
{
  struct Node *temp;
   int i=0;
    if(position==0)
    {
        head=head->next;
    return(head);
    }
    else
       { temp=head;
    while(i<position-1)
    {
        i=i+1;
        temp=temp->next;
    }
   temp->next=temp->next->next;
    return(head);
    // Complete this method
}
}