Pointers and Arrays: C programming solved programs/examples with solutions

48

Pointers and Arrays: C programming solved programs/examples with solutions | c programming questions | basic c programs | c programs for interview | pattern programs in c


  • Use pointers to write function mystrend(s, t) which takes two strings “s”, and “t” as input  and returns 1 if string “t” is present at the end in the string “s”  otherwise 0.
    1. Eg: str1 = “CMCLTD”, str2=”LTD”
      1. Res = mystrend(str1, str2) then Res = 1.

#include<stdio.h>

#define EXIT 0

#define max 50

int mystrend(char *,char *);

void reverse(char *);

int main()

{

  char s[max],t[max];int temp1;

  printf(“Enter string1:”);

  scanf(“%s”,s);

  printf(“Enter string2:”);

  scanf(“%s”,t);

 temp1= mystrend(s,t);

 if(temp1==1)

 printf(“Both are same”);

 else if(temp1==0)

 printf(“NOt same”);

}

 int mystrend(char *s,char *t)

 {

   int len1,len2,i,temp;

   char ls[max]=””;

   len1=strlen(s);

   len2=strlen(t);

    reverse(s);

    reverse(t);

   for(i=0;i<len2;i++){

           ls[i]=s[i];}

   for(i=0;i<len2;i++)

{

   if(ls[i]==t[i]){

       temp=1;}

   else{

       temp=0;}}

  return temp;

}

void reverse(char *string)

{

   int length, c;

   char *begin, *end, temp;

   length = strlen(string);

begin = string;

   end = string;

   for ( c = 0 ; c < ( length – 1 ) ; c++ )

      end++;

   for ( c = 0 ; c < length/2 ; c++ )

   {

      temp = *end;

      *end = *begin;

      *begin = temp;

      begin++;

      end–;

   }

}


  • Write a function “mystrncpy” which copies at most “n” charactors from string1 to string2. Use pointers instead of array indexing.
    • Eg: Str1 = “”, str2= “Hyderabad”
    • Str1= mystrncpy(Str1, str2, 3) then Str1 = “Hyd”

#include<stdio.h>

#include<string.h>

#define EXIT 0

char * mystrncpy(char *,char *,int);

int main()

{

 char str1[50];

 char str2[50];

 int n;

  printf(“Enter String1:”);

  scanf(“%s”,str1);

  printf(“Enter string2 which is to be coopied into string1:”);

  scanf(“%s”,str2);

  printf(“Enter the vale of n to copy atmost n characters:”);

  scanf(“%d”,&n);

  mystrncpy(str1,str2,n);

  return EXIT;

}

char * mystrncpy(char *str1,char *str2,int n)

 {

  int i,len1;

char *str;

   str=str1;

   for(i=0;i<n;i++)

    *str++=*str2++;

   printf(“The copied value into string1 from string2 is %s”,str1);

 }


  • Write a function “strncmp” which compares  at most “n” charactors of  string1 and  string2 and returns 0 if they are equal and -1 or 1 if they have difference respectively. Use pointers instead of array indexing.
  • Eg: Str1 = “CMCLtd”, str2= “Hyderabad”

Res  = mystrncmp(Str1, str2, 3) should Res = -1

#include<stdio.h>

#include<string.h>

#define EXIT 0

#define DIFF -1

#define max 50

int trncmp(char *,char *,int);

int main()

{

  char str1[max];

  char str2[max];

  int n,temp;

  printf(“Enter string1:”);

  scanf(“%s”,str1);

  printf(“Enter string2:”);

  scanf(“%s”,str2);

  printf(“Enter the vale of n to compare atmost n characters:”);

  scanf(“%d”,&n);

 temp=trncmp(str1,str2,n);

  printf(“%d”,temp);

  return EXIT;

}

int trncmp(char *str1,char *str2,int n)

  {

    int i;

    char *str;

    str=str1;

    for(i=0;i<n;i++)

    if(*str++==*str2++)

   // printf(“same”);

     return EXIT;

    else

   // printf(“different”);

      return DIFF;

  }


  • Write a  function  month_day(int  year,  int yearday,  int *pMonth,  int *pDay) where year and  yearday are inputs  and the functions writes the month and date into pMonth and pDay.

    Eg : the function call  month_day(1988,60, &m, &d) should fill  m with 2 and d with 29.

  • Write a function to get an integer from the given input buffer.  If there are ‘+’ or ‘-‘ symbols not followed by a digit then push those symbols back to the buffer.

  • Write a function to concatenate two strings using pointers

  • Use pointers to write function strend(s, t) which takes two strings “s”, and “t” as input and returns 1 if string “t” is present at the end in the string “s”  otherwise 0.

  • Write a function “strncpy” which copies at most “n” charactors from string1 to string2. Use pointers instead of array indexing.


  • Write the quick sorting program to sort given strings in decreasing order.

#include <stdio.h>

#include <string.h>

  1. In the declaration int *daytab[13] , describe daytab.
  2. Give the C-declaration for : function returing pointer to array[] of pointer to function returning char.
  3. Write a function to insert a node into a single linked list exactly in the middle.

#include<stdio.h>

#include<ctype.h>

#include<stdlib.h>

#define max 6

#define EXIT_SUCCESS 0

struct node

{

   int num;

   struct node * next;

};

void insertinmiddle(int);

void add(int);

void display();

void delete(int);

struct node * head=NULL;

struct node * end=NULL;

int main()

{

  int ch,x,y;

   do

   {

    printf(“\n\n1.Add element\n”);

    printf(“\n2.Add element in the middle\n”);

    printf(“\n3.Remove element\n”);

    printf(“\n4.Display all the elements\n”);

    printf(“\n5.Exit Program\n”);

    printf(“\nSelect any one of the above : “);

    scanf(“%d”,&ch);

    switch(ch)

    {

        case 1: printf(“\nEnter the element to add:”);

                scanf(“%d”,&x);

                add(x);

                break;

        case 2: printf(“\nEnter the element to insert in the middle:”);

                scanf(“%d”,&y);printf(“y is %d\n”,y);

                insertinmiddle(y);

                break;

        case 3: printf(“\nEnter the element to remove:”);

                scanf(“%d”,&x);

     delete(x);

                break;

        case 4: display();

                break;

        default: printf(“\n EXIT\n”);

    }

   }while(ch!=5);

   return EXIT_SUCCESS;

}

void add(int x)

{

   struct node * temp,*newnode;

   if(head==NULL)

   {

        head=(struct node*) malloc(sizeof(struct node));

        head->num=x;

        head->next=NULL;

        end=head;

   }

  else

   {

        temp=head;

        while(temp->next!=NULL)

           temp=temp->next;

        newnode=(struct node*) malloc(sizeof(struct node));

        temp->next=newnode;

        newnode->num=x;

        newnode->next=NULL;

        end=newnode;

   }

}

void insertinmiddle(int x)

{

   fflush(stdout);

   struct node * temp;

   struct node * temp1;

   struct node * newnode;

   int length=0,i=0;

temp=head;

   if(head==NULL)

        printf(“List is empty”);

   else

   {

      while(temp!=NULL)

      {

           //length++;

           temp=temp->next;

           length++;

      }

      printf(“length is %d”,length);

      temp=head;

      for(i=0;i<length/2-1;i++)

           temp=temp->next;

      temp1=temp->next;

      newnode=(struct node*) malloc(sizeof(struct node));

      temp->next=newnode;

      newnode->num=x;

      newnode->next=temp1;

   }

}

void delete(int x)

{

   if(head==NULL)

        printf(“\nList is empty\n”);

   else

   {

        struct node * temp=head,* temp1;

        if(head->num==x)

        {

           temp1=temp->next;

           head=temp1;

           free(temp);

        }

        else

          while(temp->next!=NULL)

          {

             if(temp->next->num==x)

             {

                temp1=temp->next;

                temp->next=temp1->next;

                free(temp1);

             }

             temp=temp->next;

   }

   }

}

void display()

{

   struct node * temp=head;

   if(head==NULL)

        printf(“List is empty”);

   else

   {

        do

        {

                printf(“%d “,temp->num);

                temp=temp->next;

        }while(temp!=NULL);

   }

}

Summary
C programming solved programs/examples with solutions
Article Name
C programming solved programs/examples with solutions
Description
Pointers and Arrays: C programming solved programs/examples with solutions | c programming questions | basic c programs | c programs for interview | pattern programs in c
Admin
Lazyfreshers
Lazyfreshers

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.