0% found this document useful (0 votes)
1 views5 pages

Practical9 2024 PF

Uploaded by

Jahnavi Shah
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)
1 views5 pages

Practical9 2024 PF

Uploaded by

Jahnavi Shah
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/ 5

FR.

CONCEICAO RODRIGUES COLLEGE OF ENGINEERING


Academic Term: September -December 2024
Subject Name: Programming Fundamentals
Course Code: ESC11CEL03
Class: F.E. (Sem –I)

Name of Student Tanmay Mahajan


Lab Experiment No. 9 Roll No. 10671
Date of Performance: Date of Submission:
Expt. Title a) Reversing an array using pointers
b) Write a program to read and print a text. Also count the number of
character and words in the text using Pointers.

CO Mapping: CO4

Problem Definition: Write a program using function


To reads elements in array

To displays contents of array in reverse order using pointer.

Objective of the Experiment:


1. Understanding pointers.
2. To understand how to handle arrays using pointers.

Theory:
A pointer is a variable whose value is the address of another variable ie. direct address of the
memory location. Like any variable or constant, you must declare a pointer before you can use it
to store any variable address. The general form of a pointer variable declaration is:
type *var-name;

Here, type is the pointer's base type; it must be a valid C data type and var-name is the name of
the pointer variable. The asterisk * you used to declare a pointer is the same asterisk that you
use for multiplication. However, in this statement the asterisk is being used to designate a
variable as a pointer.
Example:
int var = 20; /* actual variable declaration */
int *ip; /* pointer variable declaration */
ip = &var; /* store address of var in pointer variable*/
printf("Address of var variable: %x\n", &var ); /* address stored in pointer variable */
printf("Address stored in ip variable: %x\n", ip ); /* access the value using the pointer */
printf("Value of *ip variable: %d\n", *ip )

When we pass the address of an array while calling a function then this is called function call by
reference. When we pass an address as an argument, the function declaration should have a pointer
as a parameter to receive the passed address.
Suggested Algorithm
(You can work with any type of elements and any size)
1. Create an array a
2. Call read_array (a,n) function to populate array with data
3. Call display_array(&a[n-1],n) function to display array in reverse order.

Source code for the implementation.


#include <stdio.h>

void reverseArray(int *arr, int n) {


int *start = arr;
int *end = arr + n - 1;
while (start < end) {
int temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
}

int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
reverseArray(arr, n);
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Sample input and output
Test case 1:
Enter the number of elements: 10
Enter the elements: 1 12 45 67 89 32 43 54 76 87
Elements in reverse order are: 87 76 54 43 32 89 67 45 12 1

The program was tested for different sets of inputs.


Program is working is SATISFACTORY NOT SATISFACTORY ( Tick appropriate outcome)
b) Definition: Write a program to read and print a text. Also count the number of character
and words in the text using Pointers.

Source code for the implementation.


#include <stdio.h>
#include <ctype.h>

int main() {
char text[1000];
char *ptr;
int charCount = 0, wordCount = 0, inWord = 0;

printf("Enter text: ");


fgets(text, sizeof(text), stdin);

ptr = text;
while (*ptr != '\0') {
charCount++;
if (isspace(*ptr)) {
inWord = 0;
} else if (inWord == 0) {
inWord = 1;
wordCount++;
}
ptr++;
}

printf("Text: %s", text);


printf("Number of characters: %d\n", charCount);
printf("Number of words: %d\n", wordCount);

return 0;
}
Sample input and output
Test case 1:
Enter String: How are you
The string is: How are you
Number of character=11
Number of words=3
The program was tested for different sets of inputs.
Program is working is SATISFACTORY NOT SATISFACTORY ( Tick appropriate outcome)

Evaluation:

On time Completion and Knowledge of the topic Implementation and Total (10)
Submission (2) (4) Output (4)

Date & Signature of teacher:

You might also like