Wednesday, May 13, 2015

Remove Vowels



#include<stdio.h>
int main()
{
        char a[]="rajkamal muthusamy";
        char *p,*q,*z;
        z=a;
        p=a;
        q=a;
        while(*p)
        {
                if(*p=='a' || *p=='e' || *p=='i' || *p=='o' || *p=='u')
                {
                        q=p+1;
                        while(*p)
                        {
                                *p=*q;
                                //printf("%s\t",p);
                                //printf("%s",z);
                                q++;
                                p++;
                        }
                 p=a;
                q=a;
                }
                p++;
        }

        printf("%s",z);
}


input:
rajkamal muthusamy
output:

rjkml mthsmy

character count





#include<stdio.h>
main()
{
        char p[]={"SOUMDsoundSDUTY"};
        char a[52]={0},*k,i;
        k=p;
        while(*k!='\0')
        {
                if((*k>='A')&&(*k<='Z'))
                        a[*k-65]=a[*k-65]+1;
                if((*k>='a')&&(*k<='z'))
                        a[*k-71]=a[*k-71]+1;
                if(*k==' '&&*k==',')
                        break;
                k++;
        }
//      for(i=0;i<52;i++)
//      {
//      printf("\n%d",a[i]);
//      }
        for(i=65;i<91;i++)
        {
                if(a[i-65]!=0)
                        printf("\n%c is repeated in %d times",i,a[i-65]);
        }
        for(i=97;i<123;i++)
        {
                if(a[i-71]!=0)
                        printf("\n%c is repeated in %d times",i,a[i-71]);
        }

}

find the string




#include<stdio.h>
void stricmp(char *p,char *q);
int strilen(char*s);
main()
{
        char a[]={"the"},b[]={"the cat sat the the on the mat"};
        printf("the word is %s",a);
        printf("\nthe setence is %s",b);
        stricmp(a,b);
}
            void stricmp(char *p,char *q)
{
        char *t;
        int k,times=0,c,i;
        t=p;
        k=strilen(q);
        for(i=1; i<=k; i++)
        {
                while((*p!='\0')&&(*q!=' '))
                {
                        if(*p==*q)
                        {
                                c=1;
                        }
                        else
                        {
                                c=0;
                        }
                        q++;
                        p++;
                }
                        if(c==1)
                        {
                                times++;
                        }
                        p=t;
                        q++;
                }
        printf("\nthe word repeated at %d times",times);
}
int strilen(char *s)
{
        int len=0;
        while(*s!=0)
{
                s++;
                len++;
        }
        return len;

}

str upper to lower



#include<stdio.h>
void convert(char *p);
main()
{
        char a[]={"KANCHANA KARTHIKA"};
        convert(a);
        printf("%s",a);
}
void convert(char *p)
{
        while(*p!='\0')
        {
                if(*p!=32)
                {
                        *p=*p+32;
                }
                p++;
        }

}

singlely Linked list



#include<stdio.h>
#include<stdlib.h>
struct list
{
        int data;
        struct list *next;
};
void addnode(int data);
void insert(int data,int count);
void deletefirst();
void deletemiddle(int count);
int length();
void deletelast(int count);
int getnth(int get);
void deletelist(int count);
void display();
struct list *head=NULL;
struct list *temp;
struct list *temp1;
void addnode(int data)
{

        temp=head;
        if(head==NULL)
        {
                head=(struct list *)malloc(sizeof(struct list));
                head->data=data;
                head->next=NULL;
                head=head;
        }
        else
        {
                while(temp->next!=NULL)
                {
                        temp=temp->next;
                }
                temp1=(struct list *)malloc(sizeof(struct list));
                temp1->data=data;
                temp1->next=NULL;
                temp->next=temp1;
        }
}
void insert(int data,int count)
{
//      struct list *temp;
        struct list *new;
        int i=1;
        temp=head;
        while(i<(count-1))
        {
                temp=temp->next;
                i++;
        }
        new=(struct list *)malloc(sizeof(struct list));
        new->data=data;
        new->next=temp->next;
        temp->next=new;
}
void deletefirst()
{

        temp=head;
        temp1=head;
        temp=temp->next;
        head=temp;
        free(temp1);
}
void deletemiddle(int count)
{
        int i=1;
        temp=head;
        temp1=head;
        while(i<count)
        {
                temp1=temp1->next;
                i++;
        }
        i=1;
        while(i<(count-1))
        {
                temp=temp->next;
                i++;
        }
        temp->next=temp1->next;
        free(temp1);
}
int length()
{
        int count=0;
        temp=head;
        while(temp!=NULL)
        {
                temp=temp->next;
                count++;
        }
        return count;
}
void deletelast(int count)
{
        int i=1;
        temp=head;
        temp1=head;
        while(i<count)
                {
                        temp1=temp1->next;
                        i++;
                }
        i=1;
        while(i<(count-1))
                {
                        temp=temp->next;
                        i++;
                }
        temp->next=NULL;
        free(temp1);

}
int getnth(int get)
{
        int i=1,q;
        temp=head;
        while(i<get)
        {
                temp=temp->next;
                i++;
        }
        q=temp->data;
        return q;
}
void deletelist(int count)
{
        int a,i=1;
        temp=head;
        temp1=head;
        while(i<count)
                {
                        temp1=temp1->next;
                        i++;
                }
        i=1;
        while(i<(count-1))
                {
                        temp=temp->next;
                        i++;
                }
        temp->next=NULL;
        free(temp1);
        a=count-1;
        if(a>0)
        {
        deletelist(a);
        }
        head=NULL;
}
void display()
{
        struct list *temp;
        temp=head;
        if(temp==NULL)
        {
        printf("No records found and cannot delete");
        return;
        }
        while(temp!=NULL)
        {
                printf("%d\n",temp->data);
                temp=temp->next;
        }
}
int main()
{
        int count,options,pos,q;
while(1)
        {
                printf("Press 1 for Insert node\n");
                printf("Press 2 for delete first node\n");
                printf("Press 3 for delete node\n");
                printf("Press 4 for delete last node\n");
                printf("Press 5 for get node\n");
                printf("press 6 for delete list\n");
                printf("Press 7 for display\n");
                printf("Press 8 for exit\n");
                scanf("%d",&options);
                switch(options)
                {

case 1:
                        printf("Enter the data:\n");
                        scanf("%d",&pos);
                        addnode(pos);
                        break;
case 2:
                        deletefirst();
                        break;
case 3:
                        printf("Enter the data:\n");
                        scanf("%d",&pos);
                        deletemiddle(pos);
                        break;
case 4:
                        count=length();
                        deletelast(count);
                        break;
case 5:
                        printf("Enter the data:\n");
                        scanf("%d",&pos);
                        q=getnth(pos);
                        break;
case 6:
                        count=length();
                        deletelist(count);
                        break;
case 7:
                        display();
                        break;
case 8:
                        printf("program terminated by user\n");
                        exit(0);
                        break;
default:
                        printf("please enter the valid option\n");
                        break;


                }
                printf("do you want to continue:Press Y to continue, Press N to exit\n");
                scanf(" %c",&options);
                if(options=='y')
                 continue;
                else
                {
                        printf("program terminated by user\n");
                        exit(0);
                }
        }

}

str replace



#include<stdio.h>
int main()
{
        char a[]="complete ref book";
        char b[]="ref";
        char c[]="hear";
        char *p,*q,*s,*t,*u,*r;
        p=a;
        q=b;
        r=c;
        u=p;
        s=a;
        int i=0,j=0,count=0,length=3,length1=4;
        while(*p)
        {
                while(*p==*q)
                {
                        p++;
                        q++;
                        count++;
                        if (count == length)
                        {
                                u=p;
                                if(length<length1)
                                {
                                        while(*p)
                                        {
                                                p++;
                                        }
                                        t=p+1;
                                        while(j<length1)
                                        {
                                                while(*p!='f')
                                                {
                                                        *t=*p;
                                                        t--;
                                                        p--;
                                                }
                                                j++;
                                        }
                                }
                                u=u-length;
                                while(i<length1)
                                                                     {
                                        *u=*r;
                                        r++;
                                        u++;
                                        i++;
                                }
                                r=c;
                        }
                }
                q=b;
                p++;
                count=0;
        }
        printf("%s",s);
}

str cmp using recursive



#include<stdio.h>
#include<string.h>
int compare(char *p,char*q,int l,int m);
int main()
{
        char a[]="deepan";
        char b[]="deepika";
        char *i,*j;
        int x=strlen(a),y=strlen(b),flag;
        i=a;
        j=b;
        flag=compare(i,j,x,y);
        if(flag==1)
                printf("the strings are same");
        else
                printf("the strings are different");
}
int compare(char *p,char *q,int l,int m)
{
        static int flag=0;
        if((*p==*q)&&(l==m))
        {
                p++;
                q++;
                compare(p,q,l--,m--);
                flag=1;
        }
        else
        {
                flag=0;
        }
        return flag;

}

str rev using fn pointer with local



#include<string.h>
#include<stdlib.h>
#include<stdio.h>
/* function prototype */
char *rev(char*);
//int strlen(char*);
int main()
{
char a[10]="selva";
char *p;
/* function call*/
p=rev(a);
printf("rev e string id %s\n",p);
return 0;
}
/* function defination*/
char *rev(char *q)
{
int l=0;
static char b[10];
char *r;
r=b;
l=strlen(q);
q=q+(l-1);
while(l>=0)
{
*r=*q;
r++;
q--;
l--;
}
return r;
}

str reverse using fn pointer




#include<string.h>
#include<stdlib.h>
#include<stdio.h>
/* function prototype */
char a[10]="string";
char b[10];
void *rev();
int main()
{
char *p;
/* function call*/
p=rev();
printf("rev e string id %s\n",p);
return 0;
}
/* function defination*/
void *rev()
{
int l=0;
char *r,*q;
q=a;
r=b;
l=strlen(q);
q=q+(l-1);
while(l>=0)
{
*r=*q;
r++;
q--;
l--;
}
return (b);
}

str word rev



#include<stdio.h>
#include<string.h>
int main()
{
        char x[]="hai hello world";
        char *r,*p,*q,*t,*start,*e,temp,b[50];
        int i=0,c=0,l=0;
        p=x;
        q=x;
        t=x;
        start=x;
        e=x;
        while(*p)
        {
                if(*q !=' ')
                {
                        b[c]=*q;
                        q++;
                        c++;
                        l++;
                //      printf("input b:%s\n",b);
                }
                //printf("%d",l);
                else
                {
                        b[c]='\0';
                        r=q+1;
                        e=b+(l-1);
                        //printf("e=%c\n",*e);
                        while(i<(l/2))
                        {
                                temp=*e;
                                *e=*t;
                                *t=temp;
                                i++;
                //      printf("t=%c\n",*t);
                //      printf("e=%c\n",*e);
                        }
//                      printf("%s",t);
                        //printf(" ");
                        q=r;
                        e=t+l;
                        //printf("%c",*e);
 }
//                      printf("%s",t);
                        //printf(" ");
                        q=r;
                        e=t+l;
                        //printf("%c",*e);
                        while(i<(l/2))
                        {
                                temp=*e;
                                *e=*q;
                                *q=temp;
                                i++;
                        }
                        printf("%s",t);
                        printf(" ");
                }
                p++;
        }
        printf("%s",t);
        printf(" ");
}

String character reverse



#include<stdio.h>
int main()
{
char a;
int n,r=0,y=0;
scanf("%c",&a);
n=a;
printf("%d\n",n);
while(n>0)
{
y=n%10;
r=(r*10)+y;
n=n/10;
//printf("%d\n",r);
}
printf("%d\n",r);
printf("%c",r);
}

String reverse using recuriseve



#include<stdio.h>
#include<string.h>
void reverse(char*,int,int);
main()
{
char a[100];
gets(a);
reverse(a, 0, strlen(a)-1);
printf("%s\n",a);
return 0;
}
void reverse(char *x, int beg, int end)
{
char c;
if ( beg >= end )
return;
c = *(x+beg);
*(x+beg) = *(x+end);
*(x+end) = c;
reverse(x, ++beg, --end);
}

Middle Bit



#include<stdio.h>
main()
{
        unsigned int a=45,b=255;
        unsigned int mask=128;
        printf("\nBinary value of %d is:",a);
        binary(a,mask);
        printf("\nBinary value of %d is:",b);
        binary(b,mask);
        printf("\nAfter changing middle bit: ");
        middle(a,b);
}
binary(int x,int mask)
{
        int y;
        while(mask!=0)
        {
                y=mask&x;
                if(y==0)
                {
                        printf("0");
                }
                else
                {
                        printf("1");
                }
                mask=mask>>1;
        }
}

middle(int x,int y)
{
        unsigned int z=199;
        unsigned int mask=128;
        x=x&z;
        y=y&7;
        y=y<<3;
        x=x|y;
        binary(x,mask);
        printf("\n");

}

Bit Change



#include<stdio.h>
void showbits(unsigned char n);
int main()
{
        unsigned char num=170,num1=167,p,r,s,t,z;
        int w,q;
        printf("\nvalue of num=");
        showbits(num);
        printf("\nvalue of num1=");
        showbits(num1);
        printf("\nHow many bits you want to strip (1-7)=");
        scanf("%d",&w);
        p=w;
        printf("\nwhere do you want to change (1-7)=");
        scanf("%d",&q);
        if(p==1)
        {
                z=num1 & 0x1;
                showbits(z);
        }
        else if(p==2)
        {
                z=num1 & 0x3;
                showbits(z);
        }
        else if(p==3)
        {
                z=num1 & 0x7;
                showbits(num1);
        }
        else if(p==4)
        {
                z=num1 & 0xF;
                showbits(z);
        }
        else if(p==5)
        {
                z=num1 & 0x1F;
showbits(z);
  }
        else if(p==6)
        {
                z=num1 & 0x3F;
                showbits(z);
        }
        else if(p==7)
        {
                z=num1 & 0x7F;
                showbits(z);
        }
        else
        {
                z=num1&0XFF;
                showbits(z);
        }
        r=z<<(q-p);
        printf("\nMoved bit=");
        showbits(r);
        s=num|r;
        printf("\nvalue of new num=");
        showbits(s);
}
void showbits(unsigned char n)
{
        int i;
        unsigned char j,k,andmask;
        for(i=7;i>=0;i--)
        {
                j=i;
                andmask = 1<<j;
                k=n&andmask;
                if(k==0)
                        printf("0");
                else
                        printf("1");
        }
}