Thursday, 4 May 2017

Array - Shuffle merge

 Given a input two string (inputStr1) and (inputStr2), merge these two string by combining elements of same index Input String: "Hello" Deletion String: "123" Output String: "H1e2l3lo"


@Override
public String func (String inputStr1, String inputStr2){


                 if(inputStr1==null)
                 {
                     return inputStr2;
                 }
                 else if(inputStr2==null)
                 {
                     return inputStr1;
                 }
         try{
         int k=inputStr1.length();
         int l=inputStr2.length();int p,t,m=0;
        
                 String output="";
                 if(k>l){p=k;t=l;}else{p=l;t=k;m=1;}
          
         for(int i=0;i<p;i++)
         {
                   if(i<t)
                     {
                       output=output+inputStr1.charAt(i)+inputStr2.charAt(i);
                     }
                  else if(m==0)
                     {
                       output=output+inputStr1.charAt(i);
                     }
                  else
                     {
                       output=output+inputStr2.charAt(i);
                     }
         }
         return output;
         }
         catch(Exception e){
         
        return "NULL";
        }
    
    
   }
}

Visit : https://c-programs-world.blogspot.in for more question
Continue Reading →

Array - Split reverse

  Given a string (array of character), split the string to half and reverse each half Input: Input String: "Mike" Output: "iMek" Input String: "break" Output: "rbeka"



 public String func(String str)
    {
        // write your code here
        try{
            int len=str.length(),length;
            String result="";
            length=(len/2)-1;
            for(int i=length;i>=0;i--)
            {
             result=result+str.charAt(i);
            }
            if(len%2!=0)
            {
             result=result+str.charAt(len/2);
            for(int i=len-1;i>length+1;i--)
            {
                result=result+str.charAt(i);
            }}
        else{
            for(int i=len-1;i>length;i--)
            {
                 result=result+str.charAt(i);
            }
            }
            return result;
        }
        catch (Exception e)
        {
            String s="NULL";
            return s;
        }
   
    }

Visit : https://c-programs-world.blogspot.in for more question
Continue Reading →

Array - Run Length Decoding

public String func (String str){
// write your code here
if(str==null)
{
    return null;
}
String output="";
String l="";
char dh;
int i,j=0,k,c=0,p=0,m=str.length();
for(i=0;i<m;i++){
    if(str.charAt(i)>='0' && str.charAt(i)<='9'){
        c++;
        if(c==1)
        j=i-1;
        l=l+str.charAt(i);
        p=Integer.valueOf(l);
        continue;
         }
         else
         if(p==0)
         output=output+str.charAt(i);
        if(p!=0){
            for(k=1;k<p;k++)
            output=output+str.charAt(j);
            i--;}
            p=0;c=0;l="";
}
if(p!=0){
    for(i=1;i<p;i++)
    output=output+str.charAt(j);
}
        return output;
   

    }
}


Input: "a5br3" Output: "aaaaabrrr" Input: "a10br3a3" Output: "aaaaaaaaaabrrraaa"

Visit : https://c-programs-world.blogspot.in for more question
Continue Reading →

Array - Run Length Encoding

public String func (String str){
// write your code here
if(str==null)
{
    return null;
}
String output="";
String l="";
char dh;
int i,j=0,k,c=0,p=0,m=str.length();
for(i=0;i<m;i++){
    if(str.charAt(i)>='0' && str.charAt(i)<='9'){
        c++;
        if(c==1)
        j=i-1;
        l=l+str.charAt(i);
        p=Integer.valueOf(l);
        continue;
         }
         else
         if(p==0)
         output=output+str.charAt(i);
        if(p!=0){
            for(k=1;k<p;k++)
            output=output+str.charAt(j);
            i--;}
            p=0;c=0;l="";
}
if(p!=0){
    for(i=1;i<p;i++)
    output=output+str.charAt(j);
}
        return output;
   

    }
}

Input: "aaaaabrrr" Output: "a5br3" Input: "aaaaaaaaaabrrraaa" Output: "a10br3a3"

Visit : https://c-programs-world.blogspot.in for more question
Continue Reading →

Circular Array Rotation

Right Circular Array Rotation  

Given a String rotate the array by given number 'n'

#include <math.h>
#include <stdio.h>

int main(){
    int n; int j=-1,r=0,s=0,i,m,p;
    int k;                 //k=no.of.rotation
    int q;                //q=no.of.queries
//n=array size
    scanf("%d %d %d",&n,&k,&q);int a[n],b[n];
    for(i=0;i<n;i++)
     {   scanf("%d",&a[i]);}
    if(n>=k){
        b[0]=a[n-k];r=n-k;}
    if(n<k)
    {
        p=k/n;p=p*n;p=k-p;b[0]=a[n-p];r=n-p;
    }
    for(i=1;i<n;i++)
    {  if(n>=k){ if(r<n-1){b[i]=a[++r];}
                else if(j<n-k)  { b[i]=a[++j]; }}
     if(n<k){ if(r<n-1){b[i]=a[++r];}
                else if(j<n-p)  { b[i]=a[++j]; }}
    }
    for(i=0;i<q;i++)
    {   scanf("%d",&m);
       printf("%d\n",b[m]);
    }
    return 0;
}


Java

public String func (String str1, int n){
// write your code here
 
 if(str1==null)
 {
     return null;
 }
  String result="";int k=str1.length();
    n = n % k;
        int m=0;
    for(int i=0;i<k;i++)
    {    m=i;
        m= m - n;
        if(m< 0)
        {m = m + k;}
        result=result+str1.charAt(m);
    }
    return result;
 
 
    }


Input: "abcdefgh" n: 4 Output: "efghabcd"

Visit : https://c-programs-world.blogspot.in for more
Continue Reading →

Pangrams

Pangrams are sentences constructed by using every letter of the alphabet at least once.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>


int main() {
    int count=0;
    int c,i,a[27]={0};char b[1000];
    scanf("%[ !-z]266s",b);
   // printf("%s",b);
  /*  for(i=0;i<strlen(b);i++)
    {
        printf("%c ",b[i]);
    }printf("\n");
*/
    for(i=0;i<strlen(b);i++)
    {
        a[26]=b[i];//printf("%c ",a[26]);
        if(a[26]>=65&&a[26]<=90)
        {   c=(a[26]-64)-1;
            if(a[c]!=1)
            {
                a[c]=1;//printf("\ni is %d \n",i);
                ++count;
            }
        }
        if(a[26]>=97&&a[26]<=122)
        {    c=(a[26]-96)-1;
            if(a[c]!=1)
            {
                a[c]=1;
                ++count;
            }
        }
        if(count==26)
        {
            printf("pangram");
            goto bottom;
        }
    }
    printf("not pangram");
    bottom: 
   // printf("%d",count);
    return 0;
}



Visit : https://c-programs-world.blogspot.in for more question
Continue Reading →

Popular Posts

Recent Posts

Unordered List

Text Widget

Pages

Blog Archive

Powered by Blogger.

Sample Text

Array - Shuffle merge

  Given a input two string (inputStr1) and (inputStr2), merge these two string by combining elements of same index Input String: "He...

Followers

Search This Blog

Ordered List

Theme Support