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

Finding Pythagorean triplets in an array.



//   WORKING code in java with  time O(n^2)

import java.util.Arrays;
public class PythagoreanTripletsInArray {
public static double [] getSquare(String [] args){
double [] array = new double[args.length];
for(int i=0;i<args.length;i++){
double j=Integer.parseInt(args[i]);
array[i]=j*j;
}
return array;
}
public static void main(String [] args ){
double [] array =getSquare(args);
Arrays.sort(array);
Boolean flage = true;
for(double aInt : array){
for(int i=0,j=(array.length-1);i<j;){
double sum = array[i]+array[j]; 
if(aInt == sum){
System.out.println(Math.sqrt(aInt)+", "+Math.sqrt(array[i])+", "+Math.sqrt(array[j]));
flage = false;
break;
}
else if(aInt > sum)
i++;
else
j--;
}
}
if(flage)
System.out.print("No such solution exist");
}
}

Let me know your thoughts

Tuesday 2 October 2012

Re-arrange the odd/even to odd/even places





#include<stdio.h>

void Swap (int A[], int i, int j)
{
    int temp = A[i];
    A[i]=A[j];
    A[j]=temp;
}

int main()
{
    int A[] = {3,1,4,5,7,6,10,8};
    int i=0,j=1,n;
    n=sizeof(A)/sizeof(int);
    while(i<n)
    {
        while(i<n && A[i]%2 == 0)
            i+=2;
        while(j<n && A[j]%2 == 1)
            j+=2;
        if(i<n && j<n)
            Swap(A,i,j);
    }// end of while

    for(i=0;i<n;i++)
        printf(" %d",A[i]);

return 0;
}

Let me know your thoughts

Friday 14 September 2012

Sort an Array of 0 1& 2 in one pass






#include<stdio.h>

int main()
{
    int A[] = {2,2,2,1,1,1,0,0,0,1,2,0};
    int j,i,x,y,n,t=A[0];
    n=sizeof(A)/sizeof(A[0]);
printf("Size of array : %d\n",n);
    for(x=i=0,y=j=n-1;i<=j;)
    {
        if(t==0)
        {
            A[x]=0;
            x++;
            i++;
            t=A[i];
        }
        else if(t==2)
        {
            t=A[y];
            A[y]=2;
            y--;
            j--;
        }
        else
        {
            i++;
            t=A[i];
        }
    }

    while(x<=y)
    {
        A[x++]=1;
    }

    for(i=0;i<n;i++)
    {
        printf(" %d",A[i]);
    }
return 0;
}

Let me know your thoughts.

Wednesday 25 July 2012

Given a 2–d matrix , which has only 1’s and 0’s in it. Find the total number of connected sets in that matrix.



Explanation:
Connected set can be defined as group of cell(s) which has 1 mentioned on it and have at least one other cell in that set with which they share the neighbor relationship. A cell with 1 in it and no surrounding neighbor having 1 in it can be considered as a set with one cell in it. Neighbors can be defined as all the cells adjacent to the given cell in 8 possible directions ( i.e N , W , E , S , NE , NW , SE , SW direction ). A cell is not a neighbor of itself.


Input format :

First line of the input contains T , number of test-cases.
Then follow T testcases. Each testcase has given format.
N [ representing the dimension of the matrix N X N ].
Followed by N lines , with N numbers on each line.



Ouput format :

For each test case print one line ,  number of connected component it has.

Sample Input :

4
4
0 0 1 0
1 0 1 0
0 1 0 0
1 1 1 1
4
1 0 0 1
0 0 0 0
0 1 1 0
1 0 0 1
5
1 0 0 1 1
0 0 1 0 0
0 0 0 0 0
1 1 1 1 1
0 0 0 0 0
8
0 0 1 0 0 1 0 0
1 0 0 0 0 0 0 1
0 0 1 0 0 1 0 1
0 1 0 0 0 1 0 0
1 0 0 0 0 0 0 0
0 0 1 1 0 1 1 0
1 0 1 1 0 1 1 0
0 0 0 0 0 0 0 0

Sample output :

1
3
3
9

Constraint :

0 < T < 6 
0 < N < 1009 


=================WORKING CODE=================

#include<stdio.h>

int count_connected_set(int A[1015][1015],int i,int j,int n)
{
    A[i][j]=2;
    if(A[i-1][j-1]==1 && i>0 && j>0 && i<=n && j<=n )     //NW
        count_connected_set(A,i-1,j-1,n);
    if(A[i-1][j]==1 && i>0 && j>0 && i<=n && j<=n)       //N
        count_connected_set(A,i-1,j,n);
    if(A[i-1][j+1]==1 && i>0 && j>0 && i<=n && j<=n)//   //NE
        count_connected_set(A,i-1,j+1,n);
    if(A[i][j-1]==1 && i>0 && j>0 && i<=n && j<=n)       //W
        count_connected_set(A,i,j-1,n);
    if(A[i][j+1]==1 && i>0 && j>0 && i<=n && j<=n)       //E
        count_connected_set(A,i,j+1,n);
    if(A[i+1][j-1]==1 && i>0 && j>0 && i<=n && j<=n)     //SW
        count_connected_set(A,i+1,j-1,n);
    if(A[i+1][j]==1 && i>0 && j>0 && i<=n && j<=n)       //S
        count_connected_set(A,i+1,j,n);
    if(A[i+1][j+1]==1 && i>0 && j>0 && i<=n && j<=n)     //SE
        count_connected_set(A,i+1,j+1,n);
return 1;
}// end of count_connected_set()

int main()
{
    int i,j,t,k,n,A[1015][1015],count;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(i=1;i<=n;i++)
            for(j=1;j<=n;j++)
                scanf("%d",&A[i][j]); // geting input a test case;
        count =0;
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
                if(A[i][j]==1)
                    if(count_connected_set(A,i,j,n))
                        count++;
        }// of for
        printf("%d\n",count);
    }// end of while
return 0;
}


Let me know your thoughts.


Saturday 14 July 2012

REVERSE a string and if any charecter is repeating then keep that one comes last





#include<stdio.h>
#include<string.h>
int main()
{
    char S[20],S1[20];
    int i,x,j,l,A[26]={0},count=0;
    gets(S);            // UPPER CASE ONLY
    l=strlen(S);
    for(i=0;i<l;i++)
    {
        x=S[i]-'A'+1;
        if(!A[x])
            count++;
        A[x]++;
    }
    S1[count]=NULL;
    for(j=l-1,i=0;count>i;)
    {
        x=S[j]-'A'+1;
        if(A[x]>0)
        {
            S1[i++]=S[j];
            A[x]=0;
        }
        j--;
    }
    printf("\n> %s",S1);
return 0;
}

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.

Monday 9 July 2012

Which type of triangle is ?







#include<stdio.h>
int which_triangle(int a,int b,int c)
{
    if( !(a&&b&&c&&(a+b)>c&&(a+c)>b&&(b+c)>a) )
        return 4;
    else if(a==b && a==c)
        return 3;
    else if(a==b || a==c || b==c)
        return 2;
    else
        return 1;
}

int main()
{
    int a,b,c,x;
    printf("enter 3 side length : ");
    scanf("%d %d %d",&a,&b,&c);
    x=which_triangle(a,b,c);
    switch(x){
    case 1:
        printf("\nScalene Triangle\n");
        break ;
    case 2:
        printf("\nIsoscalene Triangle\n");
        break ;
    case 3:
        printf("\nEquilateral Triangle\n");
        break ;
    case 4:
        printf("\nError\n");
        break ;
    }// end of switch
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

To copy the contents of an array A to contents of Array B without using loops and any standard string copy functions.


#include<stdio.h>
int Copy_Array(int A[],int B[], int n,int i)
{
    if(i==n)
        return 0;
    B[i]=A[i];
    Copy_Array(A,B,n,i+1);
return 0;
}

int main()
{
    int A[10],B[10],i,n;
    printf("Enter size : ");
    scanf("%d",&n);
    printf("Enter Array : ");
    for(i=0;i<n;i++)
        scanf("%d",&A[i]);
    printf("\nArray A : ");
    for(i=0;i<n;i++)
        printf("%d ",A[i]);
    Copy_Array(A,B,n,0);
    printf("\nArray B : ");
    for(i=0;i<n;i++)
        printf("%d ",B[i]);
return 0;
}


Let me know your thoughts.

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.

Find the seed of a number.


Eg : 1716 = 143*1*4*3 =1716so 143 is the seed of 1716.
find all possible seed for a given number.

// Input   ||  OUTPUT 
//  1250        125       
//  174720      1456   

#include<stdio.h>
int main()
{
    int n,i,j,m,f=0;
    scanf("%d",&n);
    printf("\n");
    for(i=0;i<n;i++)
    {
        m=i;
        j=i;
        while(j)
        {
            m=m*(j%10);
            j=j/10;
        }
        if(m==n)
        {
            printf("\n%d",i);
            f=1;
        }
    }
    if(!f)
        printf("\nNo such number exit\n\n");
return 0;
}

Friday 6 July 2012

Find duplicates in O(n) time and O(1) extra space


Given an array of n elements which contains elements from 0 to n-1, with any of these numbers appearing any number of times.Find these repeating numbers in O(n) and using only constant memory space.

For example, let n be 7 and array be {1, 2, 3, 1, 3, 0, 6}, the answer should be 1 & 3.



#include<stdio.h>
#include<math.h>
int main()
{
    int n,A[15],i,flage,cycle=0,j,temp;
    printf("\nEnter size of array : ");
    scanf("%d",&n);
    printf("\nEnter elements of array( %d space saperated  integer) : \n",n );
    for(i=0;i<n;i++)
      scanf("%d",&A[i]);
    flage=0;j=0;
    printf("\n OUTPUT : ");

    for(i=0;i<n;i++)
    {
        if( A[abs(A[i])] >= 0)
           A[abs(A[i])]=-A[abs(A[i])];
        else
            printf(" %d",abs(A[i]) );
    }
return 0;
}

Let me know your thoughts.

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

sort an array a[n] containing 0s and 1s with time complexity n and you cant use more than 1 data structure



// TIME: O(n) || Space : O(1)
#include<stdio.h> int main() { int A[20],i,j,n,temp; printf("Enter n :"); scanf("%d",&n); for(i=0;i<n;i++) scanf("%d",&A[i]); printf("\n--"); i=0; j=n-1; while(i<j) { while(A[i]==0) i++; while(A[j]==1) j--; if(i<j) { temp=A[i]; A[i]=A[j]; A[j]=temp; i++; j--; } }// end of while printf("\n"); for(i=0;i<n;i++) printf("%d ",A[i]); printf("\n"); return 0; }

WAP to return numbered index if input is excel sheet column header name. e.g excel sheet column headers are A, B, C , D ... Z, AA, AB...AZ, BA,, etc if Input is D , output should be 4 and for AA output should be 27

// TIME : O(n)

#include<stdio.h> #include<string.h> int main() { int i=0,l,x,index=0; char S[5]; printf("Enter Column header : "); gets(S); l=strlen(S); while(i<l) { x=S[i++]-'A'+1; index=index*26+x; } printf("%d",index); return 0; }

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; }

Consider you have a grid of size m x n. There are stones placed randomly in some of the squares of this grid. Design a way to find out minimum rectangular area which covers all the stones in this grid.


Smallest Rectangle

Input :(place 1 as rock other wise 0) 5 4 0 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 0 0 0 0
Output: (1,0), (1,3), (3,0), (3,3) */
#include<stdio.h> #define MAX(a,b) (a>b?a:b) #define MIN(a,b) (a<b?a:b) int main() { int m,n,A[10][10],i,j,i1=10,i2=0,j1=10,j2=0; printf("enter m & n (m*n) :"); scanf("%d %d",&m,&n); printf("entet array (place 1 as rock other wise 0) :\n"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&A[i][j]); for(i=0;i<m;i++) for(j=0;j<n;j++) if(A[i][j]==1) { i1=MIN(i1,i); i2=MAX(i2,i); j1=MIN(j1,j); j2=MAX(j2,j); } printf("\n(%d,%d), (%d,%d), (%d,%d), (%d,%d)\n",i1,j1,i1,j2,i2,j1,i2,j2); return 0; }

The character 'a' to 'z' are encoded as 1 - 26. Given a string of digits, compute the number of valid decodings of the string. For example, both 'aa' and 'k' can be encoded as '11'. Hence num_valid_encodings('11') = 2.

recursive

#include <stdio.h> #include <string.h> int count_decode(char S[10],int n) { int x1=S[n-1]-'0',x2=S[n-2]-'0',z; if(!n) return 0; else if(n==1) { return 1; } else { z=x2*10+x1; // printf("z=%d ",z); if(z>26) return (count_decode(S,n-1)); else if(n==2) return (count_decode(S,n-1) + count_decode(S,n-2)+1); else return (count_decode(S,n-1) + count_decode(S,n-2)); } } int main() { int i,j,n; char S[10]; gets(S); n=strlen(S); printf("< %d >",count_decode(S,n)); return 0; }

atoi is a function in the C programming language that converts a string into an integer numerical representation. atoi stands for ASCII to integer. It is included in the C standard library header file stdlib.h. Its prototype is as follows:

atoi : ASCII to integer

//INPUT : 24 83 0 2
//OUTPUT : 24 83 0 2
#include<stdio.h>
#include<string.h> int atoi(int K[10],char string[30]) { 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; }
int main() { char string[30]; int i,a,A[10]; printf("\nEnter String : "); gets(string); a=atoi(A,string); printf("\nIntegers are : "); for(i=0;i<a;i++) printf("%d ",A[i]); printf("\n"); return 0; }

String Masking : Given a string"S" and we have to remove a an occurance of a small string "M" from there.


Input:
KUMAR
UM

Output:
KAR



#include<stdio.h>
#include<string.h>
int str_com(char K[20],int i,int m,char M[10])
{
    int k=0;
    for(k=0;k<m;k++)
        if(K[i+k]!=M[k])
            break;
    if(k==m)
        return 1;
    return 0;
}

int main()
{
    char S[20],M[10];
    int i,j=0,l,m,k,flage;
    gets(S);
    gets(M);
    l=strlen(S);
    m=strlen(M);
    for(i=0;i<l;i++)
    {
        if(S[i]!=M[0])
            S[j++]=S[i];
        else
            if( str_com(S,i,m,M) )
                i+=m-1;
            else S[j++]=S[i];
    }
    S[j]=NULL;
    printf("< %s >",S);
return 0;
}