Nakum Anil

PRACTICAL-SET-8

8_1.c
Write a program using pointer and function to determine the length of string.

#include<stdio.h>
#include<conio.h>
int count(char *d);
void main()
{
    char *a,b;
    clrscr();
    printf("\n\nenter the any sentens=");
    gets(a);
    b=count(a);
    printf("lenth=%d",b);
    getch();
}
int count(char *d)
{
    int e=0;
    while(*d!='\0')
    {
        d++;
        e++;
    }
    return(e);
}




8_2.c
Write a program using pointer to compare two strings.


#include<stdio.h>
#include<conio.h>
void add(char *e,char *f);
void main()
{
    char *a,*b;
    clrscr();
    printf("\n\nenter the any string=");
    gets(a);
    printf("\n\nenter the second string=");
    gets(b);
    add(a,b);
    getch();
}
void add(char *e,char *f)
{

    while(*e!='\0'||*f!='\0')
    {
        if(*e==*f)
        {
        e++;
        f++;
        }
        else
        {
        printf("\n\nstring are not same");
        goto ar;
        }
    }
    printf("\n\both string are same");
    ar:
    return(0);
}




8_3.c
 Write a program using pointer to concate two strings.


#include<stdio.h>
#include<conio.h>
void main()
{
    char *a,*b,*c,i=0;
    clrscr();
    printf("\n\nenter the any string=");
    gets(a);
    printf("\n\nenter the second string=");
    gets(b);
    while(*a!='\0')
    {


        c[i++]=*a;
        a++;

    }
    while(*b!='\0')
    {

        c[i++]=*b;

        b++;


    }
    c[i++]='\0';
    printf("\n\nconcate your string=");

    puts(c);
    getch();
}




8_4.c
Write a program using pointer to copy one string to another string.


#include<stdio.h>
#include<conio.h>
void main()
{
    char a[50],b[50],*pa,*pb;
    int i=0;
    clrscr();
    printf("given first string =");
    scanf("%s",a);
    printf("given second string =");
    scanf("%s",b);
    pa=&a[0];
    pb=&b[0];
    while(*pb!='\0')
    {
        *pa=*pb;
        pa++;
        pb++;
    }
    *pa='\0';
    printf("\ncopied string is:");
    puts(a);
    getch();
}




8_5.c
Write a program using pointer to copy one string to another string.


#include<stdio.h>
#include<conio.h>
#define max 30
void main()
{
    int size,i,arr[max];
    int *ptr;
    clrscr();
    ptr=&arr[0];
    printf("\n\nenter the size of arry=");
    scanf("%d",&size);
    printf("\n\n\nenter equal size integer value=");
    for(i=0;i<size;i++)
    {
        scanf("%d",ptr);
        ptr++;
    }
    ptr=&arr[size-1];
    printf("\n\nelement of arry is revese order");
    for(i=size-1;i>=0;i--)
    {
        printf("\nelement %dis %d",i,*ptr);
        ptr--;
    }
    getch();
}

No comments:

Post a Comment