Showing posts with label String. Show all posts
Showing posts with label String. Show all posts

Sunday 25 November 2012

write a function to check given string matches with given pattern





public class isMatchingWithWildcard {
public static void main(String [] args){
String pattern = "*abc*def*.doc*";
String str = "adsfabcxyzdefgh.do1docx";
if(isMatching(str, pattern))
System.out.print("Matching");
else
System.out.print("Not Matching");
}
public static Boolean isMatching(String str, String pattern){
int l = pattern.length();
if(pattern.lastIndexOf("*") == l-1)
pattern= (String) pattern.subSequence(0, l-1);
if(pattern.charAt(0) == '*')
pattern= (String) pattern.subSequence(1, pattern.length());
pattern = pattern.replace("*", "__");
String [] patternArray = pattern.split("__");
String sorttenString =str;
for(String aPattern:patternArray){
String [] temp = sorttenString.split(aPattern);
if(temp.length==1 && (aPattern == patternArray[patternArray.length-1] 
&& !sorttenString.toLowerCase().contains(aPattern.toLowerCase())))
return false;
str.substring(temp[0].length()+aPattern.length());
}
return true;
}
}

Let me know your thoughts

Tuesday 10 July 2012

WAP to find the position of a given word in a given text


#include<stdio.h>
#include<string.h>
int main()
{
    char S[100],W[20];
    int i,j,l,m,flage=0;
    printf("Enter Text : ");
    gets(S);
    printf("\nEnter Word : ");
    gets(W);
    l=strlen(S);
    m=strlen(W);
    for(i=0;i<l;i++)
    {
        if(S[i]==W[0])
        {
            for(j=1;j<m;j++)
            {
                if(S[i+j]!=W[j])
                    break ;
            }
            if(j==m)
            {
                if(!flage)
                    printf("\nPosition : ");
                flage =1;
                printf(" %d",i);
                //flage=
            }
        }
    }
    if(!flage)
        printf("\nNot found \n");
return 0;
}
Let me know your thoughts.

Sunday 8 July 2012

Find the number of substrings of a string that are palindromes


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

int is_palindrom(char S[20],int f,int l)
{
    for(;f<=l;f++,l--)
    {
        if(S[f]!=S[l])
            return 0;
    }
return 1;
}
int main()
{
    char S[20];
    int i,l,j,k,count=0;
    gets(S);
    l=strlen(S);
    for(i=0;i<l;i++)
    {
        for(j=i+1;j<l;j++)
        {
            if(S[i]==S[j])
                if(is_palindrom(S,i,j))
                {
                    printf("\n");
                    count++;
                    for(k=i;k<=j;k++)
                        printf("%c",S[k]);
                }
        }
    }
    printf("\n\nTotal << %d >> no of palindrom in given string\n",count);
return 0;
}

Let me know your thoughts.

Saturday 7 July 2012

Verify if the given password is valid or not


Verify if the given password is valid or not.
for a valid password it should follow these 2 condition
1. must be 5-12 characters long
2. must contain atleast one number and one lowercase & one upper character and a symbol



#include<stdio.h>
#include<string.h>
int main()
{
    char S[20],c;
    int i=0,l,L=0,U=0,N=0,Sy=0;
    gets(S);
    l=strlen(S);
    if(l>12 || l<5)
    {
        printf("\n\Invalid password \n");
        return 0;
    }
    while(S[i++]!=NULL)
        if(S[i-1]>='a'&&S[i-1]<='z')
            L=1;
        else if(S[i-1]>='A'&&S[i-1]<='Z')
            U=1;
        else if(S[i-1]>='0'&&S[i-1]<='9')
            N=1;
        else Sy=1;
    if(L&&U&&N&&Sy)
        printf("\nvalid \n");
    else printf("\n\Invalid password \n");
return 0;
}

Let me know your thoughts.

Friday 6 July 2012

Kahan We Met



Geet is travelling to meet her boyfriend in Mumbai.
Aditya is travelling to Mumbai for his business meeting.
Geet starts from Delhi while Aditya starts from Bangalore. Both of them are travelling on bus passing from town to town.
They meet at a nondescript city and end up travelling together to Mumbai. By the end of the journey they fall in love and settle down.
Enough of Bollywood stories, The problem at hand is to find Kahan they met?

For all interested programmers, Represent this as a programming problem(Data Structure) and find the location where Geet and Aditya would have met.

P.S. No need to actually find any city on the map where they met.
*/
//   ===========================================================================
//      Input Formate :
//
//            12 13 14 21 22            (these are 5 city pincode(2 disit integer pincoede for each city they pass through))
//
//            31 32 33 42 20 21 22      (these are 7 city pincode )
//
//     Output : 21                      (pincode of the city where they will met)
//   ===================================================================


#include<stdio.h>


int kahan_they_met(int A[20],int G[20],int g,int a)
{
    int i_g=0,i_a=0;
    if(g>a)
          i_g+=g-a;
    else  i_a+=a-g;


    while(i_g <= g)
    {
        if(A[i_a]==G[i_g])
            return A[i_a];
        i_g++;
        i_a++;
    }
return 0;
}


int convert_input(int K[],char string[200])
{
    int j=0,i=0,temp=0;
    while(string[i]!='\0')
    {
        temp=0;
        while(string[i]!=' ' && string[i]!='\0')
            temp=temp*10 + (string[i++]-'0') ;
        if(string[i]==' ')
            i++;
        K[j++]=temp;
    }


return j-1;
}




int main()
{
    char string[200];
    int g,a,i,G[20],A[20],met;
    printf("\nEnter city beetween D and M for GEET (eg: 12 13 14 21 22 ) : ");
    gets(string);
    g=convert_input(G,string);


    printf("\n-----------------\nEnter city beetween B and M for  for Aditya visit : ");
    gets(string);
    a=convert_input(A,string);


//    for(i=0;i<=g;i++)
//        printf("\n%d>>%d",i,G[i]);


   met=kahan_they_met(A,G,g,a);


   if(met)
        printf("\n\nGeet and Aditya will met at : %d\n",met);
    else
        printf("\n\nGeet and Aditya will met at Mumbai\n");


return 0;


}

Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees. Can you do this in place?




INPUT  >   OUTPUT        INPUT    >  >    OUTPUT  
4                                        4                                         
1 2 3 4        1 1 1 1           11 12 13 14      41 31 21 11
1 2 3 4        2 2 2 2           21 22 23 24      42 32 22 12
1 2 3 4        3 3 3 3           31 32 33 34      43 33 23 13
1 2 3 4        4 4 4 4           41 42 43 44      44 34 24 14



#include<stdio.h>
void rotate(int I[10][10],int O[10][10],int j,int i)
{
    int k,l,temp;
    for(k=0;k<j;k++)
    {
        temp=I[k+i][i];              // copy of Left in temp
        O[k+i][i]=I[i+j-1][i+k];     // copy bottom to left
        O[i+j-1][i+k]=I[i+j-k-1][i+j-1];   // copy right to bottom
        O[i+j-k-1][i+j-1]=I[i][i+j-k-1];   // copy top to right
        O[i][i+j-k-1]=temp;            // copy temp(left) to top
    }
}// end of rotate

int main()
{
    int I[10][10],O[10][10],i,j,N;
    scanf("%d",&N);
    for(i=0;i<N;i++)
    {
        for(j=0;j<N;j++)
            scanf("%d",&I[i][j]);
    }
    i=0;
    j=N;
    while(j>0)
    {
        rotate(I,O,j,i);  // i is base,  j is size
        j=j-2;
        i++;
    }
    printf("\n\n");
    for(i=0;i<N;i++)
    {
        printf("\n");
        for(j=0;j<N;j++)
            printf("%d ",O[i][j]);
    }
return 0;
}

Reduce aabcccccaaa to a2b1c5a3 for new string is of smaller size otherwise live it original




#include<stdio.h>
#include<string.h>
int main()
{
     char s[20],o[20],temp;
     int i=0,j=0,c=0;
     gets(s);
     while(s[i]!=NULL)
     {
         temp=s[i];
         while(s[i]== temp)
         {
             i++;
             c++;
         }
         o[j++]=temp;
         o[j++]=c+48;// ASCII of 0 is 48
         c=0;
     }// end of while
     if(strlen(o)<strlen(s))
        puts(o);
    else
        puts(s);
return 0;
}

Write a method to decide if two strings are premutation of each other or not.


#include<stdio.h>
#include<string.h>
int check_permutation(char s1[], char s2[])
{
    int c[27],i;
    for(i=0;i<27;i++)
        c[i]=0;
    if(strlen(s1)!=strlen(s2))
        return 0;
    for(i=0;s1[i]!=NULL;i++)
    {
        c[0]= s1[i];
        if(c[0]>96)
             c[0]=c[0]-96;
        else c[0]=c[0]-64;
        c[c[0]]++;

        c[0]= s2[i];
        if(c[0]>96)
             c[0]=c[0]-96;
        else c[0]=c[0]-64;
        c[c[0]]--;
    }
    for(i=1;i<27;i++)
    {
        if(c[i]!=0)
            return 0;
    }
    return 1;
}

int main()
{
    char s1[20],s2[20];
    scanf("%s%s",&s1,&s2);
    if( check_permutation(s1,s2) )
       printf("\n Permutation of each other");
    else
        printf("\n NOT a Permutation ");
return 0;
}

Implement an algorithm to determine if a string has all unique characters.


#include<stdio.h>
#include<string.h>
int main()
{
    char input[27];
    int i,l,check[28];
    scanf("%s",&input);
    l=strlen(input);
    for(i=0;i<27;i++)
        check[i]=0;
    for(i=0;i<l;i++)
    {
        check[27]=input[i];
        if(check[27]>96)
            check[27]=check[27]-96;
        else
            check[27]=check[27]-64;
        if(!check[check[27]])
            check[check[27]]=1;
        else
        {
            printf("\nNOT");
            return 0;
        }
    }
    printf("\n unique");
    return 0;
}

Write a C program to reverse all words but not string.


// Input :   My name is Kumar
// Outout: yM eman is ramuK

#include<stdio.h>
int main()
{
    char S[50];
    int i,j,t;
    printf("\nEnter Sentence : ");
    gets(S);
    printf("\nOUTPUT : ");
    for(i=0;S[i-1]!='\0';i++)
    {
        j=i;
        while(S[j]!=' ' && S[j]!=NULL)
            j++;
        t=j;
        while(j>=i)
            printf("%c",S[j--]);
        printf(" ");
        i=t;
    }
    printf("\n");
return 0;
}

Thursday 5 July 2012

In a given String Find the alphabet Which occur most.



        //  Time  ::  O(n)
    //INPUT : asd adg s
    //OUTPUT : a

#include<stdio.h>
#include<string.h>
int main()
{
    char S[20];
    int i=0,j,A[26]={0},C[2]={0,0};
    gets(S);
    printf("%s",S);

    while(S[i]!='\0')
    {
        if(S[i++]==' ')
            continue ;
        j=S[i-1]-'a';
        A[j]++;
        if(C[1]<A[j])
        {
            C[0]=j;
            C[1]=A[j];
        }
    }
    printf("\nOUTPUT : %c\n",C[0]+'a');
return 0;
}

Wednesday 4 July 2012

WAP to return all anagrams from the array of strings. (all possible permutation of a String)


#include<stdio.h> #include<string.h> int anagram(char S[10],int l,char C[10]) { char D[10]; int j,k,m=strlen(C),i; for(j=0;j<=m;j++) { for(k=m;k>j;k--) D[k]=C[k-1]; D[k]=S[m]; D[m+1]=NULL; for(k=j-1;k>=0;k--) D[k]=C[k]; if((m+1)==l) printf("%s\n",D); else anagram(S,l,D); } return 0;
} int main() { char S[10],C[10]; C[0]=NULL; gets(S); printf("\n"); anagram(S,strlen(S),C); return 0; }