0% found this document useful (0 votes)
46 views8 pages

Exercise 23 HarshKumar 23

The document contains C program code to perform various operations using pointers such as: 1) Adding two matrices by passing the resultant matrix to a function using pointers. 2) Finding the length of a string using a pointer to traverse the string. 3) Concatenating two strings by modifying the pointer values. 4) Sorting an array in ascending and descending order by passing a pointer to a comparison function. 5) Computing the sum of all elements in an array by dereferencing the pointer during traversal. 6) Counting the vowels and consonants in a string by checking the character pointed to by a pointer.

Uploaded by

Harsh kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views8 pages

Exercise 23 HarshKumar 23

The document contains C program code to perform various operations using pointers such as: 1) Adding two matrices by passing the resultant matrix to a function using pointers. 2) Finding the length of a string using a pointer to traverse the string. 3) Concatenating two strings by modifying the pointer values. 4) Sorting an array in ascending and descending order by passing a pointer to a comparison function. 5) Computing the sum of all elements in an array by dereferencing the pointer during traversal. 6) Counting the vowels and consonants in a string by checking the character pointed to by a pointer.

Uploaded by

Harsh kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

EXERCISE 23(POINTER)

138. Write a function using pointers to add two matrices and to return the resultant
matrix to the calling function.
Program:
#include <stdio.h>
#include <conio.h>
int a[5][5], b[5][5], row, col;
void add(int (*)[5]);
int main()
{
    int c[5][5], i, j;
    clrscr();
    printf("Enter row : ");
    scanf("%d", &row);
    printf("Enter column : ");
    scanf("%d", &col);
    printf("Enter matrix A :\n");
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < col; j++)
        {
            scanf("%d", &a[i][j]);
        }
    }
    printf("Enter matrix B :\n");
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < col; j++)
        {
            scanf("%d", &b[i][j]);
        }
    }
    add(c);
    printf("Addition :\n");
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < col; j++)
        {
            printf("%d\t", c[i][j]);
        }
        printf("\n");
    }
    getch();
    return 0;
}
void add(int c[5][5])
{
    int i, j;
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < col; j++)
        {
            c[i][j] = a[i][j] + b[i][j];
        }
    }
}

Output:
139. C program to find length of a string using pointer
Program:
#include<stdio.h>
int main(){
   
    char str[50] = "Hello";
    int c=0;
    char *ptr;
    ptr = &str[0];
    while((*ptr) != '\0'){
        c++;
        ptr ++;
    }
    printf("Lenght of string : %d",c);
    return 0;
}

Output:
Lenght of string : 5
140. C program to copy one string to another string using pointer.
Program:

Output:

141. C program to concatenate two strings using pointer


Program:
#include <stdio.h>
int main()
{
    char str1[100], str2[100];

    printf("\nEnter the first string: ");


    gets(str1);  

    printf("\nEnter the second string to be concatenated: ");


    gets(str2);  

    char *a = str1;
    char *b = str2;

   
    while(*a)  
    {
        a++;    
    }
    while(*b)  
    {
        *a = *b;
        b++;
        a++;
    }
    *a = '\0';  
    printf("\n\n\nThe string after concatenation is: %s ", str1);
    return 0;
}

Output:
Enter the first string: hello

Enter the second string to be concatenated: world

The string after concatenation is: helloworld


142. C program to compare two strings without using string library functions.
Program:
#include<stdio.h>
int main()
{

    char str1[20],str2[20],i,j,flag=0;

    printf("\nEnter first string :: ");


    gets(str1);
    printf("\nEnter Second string :: ");
    gets(str2);

    i=0;
    j=0;

    while(str1[i]!='\0')
    {
       i++;
    }

    while(str2[j]!='\0')
    {
       j++;
    }

    if(i!=j)
    {
        flag=0;
    }
    else
    {
         for(i=0,j=0;str1[i]!='\0',str2[j]!='\0';i++,j++)
         {
             if(str1[i]==str2[j])
             {
                flag=1;
             }
         }
    }

    if(flag==0)
    {
        printf("\nStrings are not equal\n");
    }
    else
    {
        printf("\nStrings are equal.\n");
    }

     return 0;

 }

Output:
Enter first string :: hello

Enter Second string :: hello

Strings are equal.

143. C program to sort an array using pointers.


Program:
#include <stdio.h>

#define MAX_SIZE 100

void inputArray(int * arr, int size);


void printArray(int * arr, int size);

int sortAscending(int * num1, int * num2);


int sortDescending(int * num1, int * num2);

void sort(int * arr, int size, int (* compare)(int *, int *));

int main()
{
    int arr[MAX_SIZE];
    int size;

    printf("Enter array size: ");


    scanf("%d", &size);
    printf("Enter elements in array: ");
    inputArray(arr, size);

    printf("\n\nElements before sorting: ");


    printArray(arr, size);

    printf("\n\nArray in ascending order: ");


    sort(arr, size, sortAscending);
    printArray(arr, size);

    printf("\nArray in descending order: ");


    sort(arr, size, sortDescending);
    printArray(arr, size);

   
    return 0;
}

void inputArray(int * arr, int size)


{
    int * arrEnd = (arr + size - 1);

    while(arr <= arrEnd)


        scanf("%d", arr++);
}

void printArray(int * arr, int size)


{
    int * arrEnd = (arr + size - 1);

    while(arr <= arrEnd)


        printf("%d, ", *(arr++));
}

int sortAscending(int * num1, int * num2)


{
    return (*num1) - (*num2);
}

int sortDescending(int * num1, int * num2)


{
    return (*num2) - (*num1);
}

void sort(int * arr, int size, int (* compare)(int *, int *))


{
    int * arrEnd  = (arr + size - 1);

    int * curElem = arr;


    int * elemToSort;

    while(curElem <= arrEnd)


    {
        elemToSort = curElem;

        while(elemToSort <= arrEnd)


        {
            if(compare(curElem, elemToSort) > 0)
            {
                *curElem    ^= *elemToSort;
                *elemToSort ^= *curElem;
                *curElem    ^= *elemToSort;
            }

            elemToSort++;
        }

        curElem++;
    }
}

Output:
Enter array size: 10
Enter elements in array: 10 -1 0 4 2 100 15 20 24 -5
Elements before sorting: 10, -1, 0, 4, 2, 100, 15, 20, 24, -5,

Array in ascending order: -5, -1, 0, 2, 4, 10, 15, 20, 24, 100,
Array in descending order: 100, 24, 20, 15, 10, 4, 2, 0, -1, -5,

144. Write a program in C to compute the sum of all elements in an array using
pointers.
Program:
#include <stdio.h>
#include <malloc.h>
void main(){
   int i, n, sum = 0;
   int *ptr;
   printf("Enter size of array : \n");
   scanf("%d", &n);
   ptr = (int *) malloc(n * sizeof(int));
   printf("Enter elements in the List \n");
   for (i = 0; i < n; i++){
      scanf("%d", ptr + i);
   }
   //calculate sum of elements
   for (i = 0; i < n; i++){
      sum = sum + *(ptr + i);
   }
   printf("Sum of all elements in an array is = %d\n", sum);
   return 0;
}

Output:
Enter size of array :
5
Enter elements in the List
1
2
3
4
5
Sum of all elements in an array is = 15

145. Write a program in C to count the number of vowels and consonants in a


string using a pointer.
Program:
#include <stdio.h>
#include <string.h>
int main()
{
    char s[1000], *p;
    int vowels = 0, consonants = 0;

    printf("Enter  the string : ");


    gets(s);

    p = s;

    while (*p)
    {
        if ((*p >= 65 && *p <= 90) || (*p >= 97 && *p <= 122))
        {

            if (*p == 'a' || *p == 'e' || *p == 'i' || *p == 'o'


|| *p == 'u' || *p == 'A' || *p == 'E' || *p == 'I' || *p == 'O'
|| *p == 'U')
                vowels++;
            else
                consonants++;
        }
        p++;
    }

    printf("vowels = %d\n", vowels);


    printf("consonants = %d\n", consonants);

    return 0;
}

Output:
Enter  the string : AEIOU aeiou
vowels = 10
consonants = 0

You might also like