Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

c program using pointer (2)

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

c program using pointer (2)

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

// C program to check if a string is palindrome

// using pointers

#include <stdio.h>

// Function to check if the string is palindrome


// using pointers
void isPalindrome(char* string)
{
char *ptr, *rev;

ptr = string;

while (*ptr != '\0') {


++ptr;
}
--ptr;

for (rev = string; ptr >= rev;) {


if (*ptr == *rev) {
--ptr;
rev++;
}
else
break;
}

if (rev > ptr)


printf("String is Palindrome");
else
printf("String is not a Palindrome");
}

// Driver code
int main()
{
char str[1000] = "madam";

isPalindrome(str);

return 0;
}

Output
String is Palindrome

C Program to Compare Two Strings Using Pointers


#include <iostream>
using namespace std;

int main()
{
char string1[50],string2[50],*str1,*str2;
int i,equal = 0;

printf("Enter The First String: ");


scanf("%s",string1);

printf("Enter The Second String: ");


scanf("%s",string2);

str1 = string1;
str2 = string2;

while(*str1 == *str2)
{
if ( *str1 == '\0' || *str2 == '\0' )
break;

str1++;
str2++;
}

if( *str1 == '\0' && *str2 == '\0' )


printf("\n\nBoth Strings Are Equal.");
else
printf("\n\nBoth Strings Are Not Equal.");
}

#include <stdio.h>

// Function to reverse the string using pointers


void reverseString(char *start) {
char *end = start, temp;

// Move the 'end' pointer to the last character of the string


while (*end) {
end++;
}
end--; // Move back from null terminator to the last character

// Swap characters from start and end, moving them towards each other
while (start < end) {
temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
}

int main() {
char input[100];

// Ask the user for the string input


printf("Enter a string: ");
gets(input); // Reads the string input

reverseString(input);

printf("Reversed string: %s\n", input);

return 0;
}
Output:
Enter a string: programming
Reversed string: gnimmargorp

#include <stdio.h>

int main() {
int arr[5]; // Declare an array of integers
int *ptr; // Declare an integer pointer
int sum = 0; // Initialize sum
int i;

// Input array elements


printf("Enter 5 elements of the array: ");
for(i = 0; i < 5; i++) {
scanf("%d", &arr[i]);
}

// Use the pointer to traverse and compute the sum


ptr = arr; // Point to the first element of the array
for(i = 0; i < 5; i++) {
sum += *ptr; // Add the value pointed by ptr to sum
ptr++; // Move the pointer to the next array element
}

// Display the result


printf("Sum of array elements: %d\n", sum);

return 0;
}
Output:
Enter 5 elements of the array: 1 2 3 4 5
Sum of array elements: 15

#include<stdio.h>
#include<conio.h>
void main()
{
int max,i,*a[15],n;
clrscr();
printf(“enter no. of element for the array: ”);
scanf(“%d”,&n);
printf(“enter element for the array: ”);
for(i=0;i<n;i++)
scanf(“%d”,&*a[i]);
max=*a[0];
for(i=1;i<n;i++)
{
if(max<*a[i]) max=*a[i];
}
printf(“maximum no= %d”,max);
getch();

Output:
enter no. of element for the array:6
enter elements for array:
5
4
7
1
2
maximum no= 7 **************************

Program

#include<stdio.h>

void main()
{
int a[10][10], b[10][10], c[10][10];

int m, n, i, j;

printf("Enter size of the matrix\n");


scanf("%d %d", &m, &n);

printf("Enter elements of matrix 'a' of size %d\n", (m * n));

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


for(j = 0; j < n; j++)
scanf("%d", (*(a + i) + j));

printf("Enter elements of matrix 'b' of size %d\n", (m * n));


for(i = 0; i < m; i++)
for(j = 0; j < n; j++)
scanf("%d", (*(b + i) + j));

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


for(j = 0; j < n; j++)
(*(*(c + i) + j)) = (*(*(a + i) + j)) + (*(*(a + i) + j));

printf("The sum of matrix is:\n");


for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
printf("%d\t", (*(*(c + i) + j)));
}
printf("\n");
}
}

Output

Enter size of the matrix


3
3
Enter elements of matrix 'a' of size 9
12
32
14
15
73
24
27
35
42
Enter elements of matrix 'b' of size 9
54
62
82
54
32
44
23
11
91
The sum of matrix is:
24 64 28
30 146 48
54 70 84

C program- To subtract two matrices using pointer

#include<stdio.h>
void main()
{
int a[5][5],b[5][5],c[5][5],i,j,m,n;
printf("Enter m & n:");
scanf("%d %d",&m,&n);
printf("Enter 1 matrix:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",(*(a+i)+j));
}
}
printf("\nEnter 2 matrix:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",(*(b+i)+j));
}
}
printf("\nSubtraction Matrix:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
*(*(c+i)+j)=*(*(a+i)+j)- *(*(b+i)+j);
printf("%d ",*(*(c+i)+j));
}
printf("\n");
}
}

Matrix multiplication using pointers in c

#include
int mul(int *a[3][3], int *b[3][3]);
int i,j,k,*c[3][3],*a[3][3],*b[3][3];
int main()
{
printf("enter the elements of 1st 3*3 matrix:A");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the elements of 1st 3*3 matrix:B");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
}
mul(a,b);
printf("result=");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\t%d\t",*c[i][j]);
}
printf("\n");
}
}
int mul(int *a[3][3], int *b[3][3])
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
*c[i][j]=0;
for(k=0;k<3;k++)
{
c[i][j] = *a[i][k] * *b[k][j] + *c[i][j];
}
}
}
}
// C program to get the length of a string using pointers
#include <stdio.h>
int main() {
char str[100], * ptr;
int count;
printf("Enter any string: ");
gets(str);
// ptr pointing to first char of string
ptr = str;
// Initialize count to zero
count = 0;
// Run until null character is reached
while ( *ptr != '\0') {
count++;
ptr++;
}
printf("The length of the string is: %d", count);
return 0;
}
Output:
Enter any string: Hello World
The length of the string is: 11

\* C Program to to find the mean of n number using pointer *\

# include < stdio.h >


int main( )
{
int a[20], n, i, sum=0 ;
float mean ;
int *ptr ;
printf(" How many Numner you want to enter: ") ;
scanf("%d ", & n) ;
printf("\n Enter the number : \n") ;

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


{
scanf("%d ", & a[i]) ;
ptr++ ;
}
ptr = & a[0] ;
printf(" \n Element in array are :\n ") ;
for (i = 0; i < n ; i++ )
{
printf("\t %d ", ( *ptr )) ;
sum = sum + *ptr ;
ptr++ ;
}
mean = sum / n ;
printf("\n Mean of %d Numbers are : %f ", n, mean) ;
return ( 0 );

You might also like