0% found this document useful (0 votes)
3 views2 pages

Pointer

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

Pointer

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

/*Q.

1 Problem Statement :
Write a program to swap two integers using pointers.*/

#include <stdio.h>
// Function to swap two integers using pointers
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}

int main() {
int x, y;
// Input two integers
printf("Enter the first integer: ");
scanf("%d", &x);
printf("Enter the second integer: ");
scanf("%d", &y);
// Display original values
printf("\nBefore swapping:\n");
printf("x = %d, y = %d\n", x, y);
// Call the swap function
swap(&x, &y);
// Display swapped values
printf("\nAfter swapping:\n");
printf("x = %d, y = %d\n", x, y);
return 0;
}

//OUTPUT

Enter the first integer: 5


Enter the second integer: 9

Before swapping:
x = 5, y = 9

After swapping:
x = 9, y = 5

Process returned 0 (0x0) execution time : 4.529 s


Press any key to continue.
/*Q.2 Problem Statement :

Write a program to find the length of a string using pointers*/

#include <stdio.h>
int stringLength(const char *str) {
const char *ptr = str; // Pointer to the start of the string
int length = 0;

// Traverse the string until the null terminator is found


while (*ptr != '\0') {
length++;
ptr++; // Move to the next character
}

return length;
}

int main() {
char str[100];
// Input the string
printf("Enter a string: ");
scanf("%99[^\n]", str); // Read input with spaces

// Calculate the length using the stringLength function


int length = stringLength(str);
// Display the length
printf("The length of the string is: %d\n", length);
return 0;
}

//OUTPUT

Enter a string: www.cprog.com


The length of the string is: 13

Process returned 0 (0x0) execution time : 8.529 s


Press any key to continue.

You might also like