0% found this document useful (0 votes)
159 views

Lab Report 8

The document contains details about an experiment on programming with pointers, strings, and function calls by reference. It includes 10 problems related to these topics and the code snippets to solve each problem. The problems cover finding the largest number among three using pointers, calculating the sum of array elements using pointers, swapping variable values using a pointer function, counting words and characters in a sentence, removing spaces from a string, combining two strings, comparing two strings, sorting strings alphabetically, and printing a word in a pyramid pattern.

Uploaded by

Shamim Reza
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)
159 views

Lab Report 8

The document contains details about an experiment on programming with pointers, strings, and function calls by reference. It includes 10 problems related to these topics and the code snippets to solve each problem. The problems cover finding the largest number among three using pointers, calculating the sum of array elements using pointers, swapping variable values using a pointer function, counting words and characters in a sentence, removing spaces from a string, combining two strings, comparing two strings, sorting strings alphabetically, and printing a word in a pyramid pattern.

Uploaded by

Shamim Reza
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/ 18

ID: 1

Name:

Green University of Bangladesh


Department of Electrical and Electronic Engineering

CSE-102
Computer Programming Laboratory

Student ID
Student Name
Section EA-221

Name of the Program EEE(Weekend)

Name of the Department EEE


ID: 2
Name:

Experiment No: 08
Name of the Experiment: Understanding programming using string and
pointers
Objective(s):
To understand programming with Pointer, String and Function call by reference.
Learning outcome(s):

Students will learn about basic string operations. They will be able to use pointers to manipulate the
elements of an array or string.

Problems:
Problem 1:

//Write a program to find biggest among three numbers using pointer.

#include <stdio.h>

int main()

int num1, num2, num3;

int *p1, *p2, *p3;

//taking input from user

printf("Enter First Number: ");

scanf("%d",&num1);

printf("Enter Second Number: ");

scanf("%d",&num2);

printf("Enter Third Number: ");

scanf("%d",&num3);

//assigning the address of input numbers to pointers

p1 = &num1;

p2 = &num2;
ID: 3
Name:

p3 = &num3;

if(*p1 > *p2){

if(*p1 > *p3){

printf("\n%d is the largest number\n", *p1);

else{

printf("\n%d is the largest number\n", *p3);

else{

if(*p2 > *p3){

printf("\n%d is the largest number\n", *p2);

else{

printf("\n%d is the largest number\n", *p3);

return 0;

Output:
ID: 4
Name:

Problem 2:

// Write a program to find the sum of all the elements of an array using pointers.

#include <stdio.h>

int main()

int arr[100],size,sum=0;

printf("Enter size of the array: ");

scanf("%d",&size);

printf("Enter the elements of the array: ");

for(int i=0; i<size; i++)

scanf("%d",&arr[i]);

//calculating sum of entered array elements

for(int i=0; i<size; i++)

sum+=arr[i];

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

return 0;

}
ID: 5
Name:

Output:

Problem 3:

/*Write a program to swap value of two variables using pointer*/

#include <stdio.h>

// function to swap the two numbers

void swap(int *x,int *y)

int t;

t = *x;

*x = *y;

*y = t;

int main()

int num1,num2;
ID: 6
Name:

printf("Enter value of num1: ");

scanf("%d",&num1);

printf("Enter value of num2: ");

scanf("%d",&num2);

//displaying numbers before swapping

printf("Before Swapping: num1 is: %d, num2 is: %d\n",num1,num2);

//calling the user defined function swap()

swap(&num1,&num2);

//displaying numbers after swapping

printf("After Swapping: num1 is: %d, num2 is: %d\n",num1,num2);

return 0;

Output:
ID: 7
Name:

Problem 4:

/*Write a program to read a sentence and count the number of characters &words in that
sentence.*/

#include <stdio.h>

#include <stdlib.h>

int main()

char str[100];//declare and initialize char array

int i;

int words=1,characters=0,space=0;//display and initiolize variables

printf("Please enter the string \n");

gets(str);//stored the string entered by user

for(i=0; str[i] != '\0'; i++){

if(str[i]!=' '){ // check characters

characters++;

else if(str[i]==' ' || str[i] != '\n' || str[i] != '\t'){ //check words

words++;

printf("\nTotal words: %d ",words); //display words

printf("\nTotal characters: %d",characters);//display characters

printf("\nSpace: %d ",(words-1));//display space


ID: 8
Name:

getch();

return 0;

Output:

Problem 5:

//Write a program to read a sentence & delete all the white spaces. Replace all “.” by “:”.

#include <stdio.h>

#include <string.h>

int main()

char s[1000];
ID: 9
Name:

int i,k=0;

printf("Enter the string : ");

gets(s);

for(i=0;s[i];i++)

s[i]=s[i+k];

if(s[i]==' '|| s[i]=='\t')

k++;

i--;

printf("string after removing all blank spaces:");

printf("%s",s);

return 0;

}
ID: 10
Name:

Output:

Problem 6:

//Write a program to copy one string to another string without using string handling function

#include<stdio.h>

int main() {

char s1[100], s2[100];

int i;

printf("\nEnter the string :");

gets(s1);

i = 0;

while (s1[i] != '\0') {

s2[i] = s1[i];

i++;

s2[i] = '\0';

printf("\nCopied String is %s ", s2);


ID: 11
Name:

return (0);

Output:

Problem 6:

//Write a program to copy one string to another string using string handling function

#include <stdio.h>

#include <string.h>

int main()

char source[1000], destination[1000];

printf("Input a string\n");

gets(source);

strcpy(destination, source);

printf("Source string: %s\n", source);


ID: 12
Name:

printf("Destination string: %s\n", destination);

return 0;

Output:

Problem 7:

#include <stdio.h>

#include <string.h>

int main()

char s1[1000],s2[1000];

int i,j;

printf("Enter string1: ");

gets(s1);

printf("Enter string2: ");


ID: 13
Name:

gets(s2);

j=strlen(s1);

for(i=0;s2[i]!='\0';i++)

s1[i+j]=s2[i];

s1[i+j]='\0';

printf("combined two strings ='%s'\n",s1);

return 0;

Output:

Problem 8:

#include <stdio.h>

#include <string.h>

int main()
ID: 14
Name:

char s1[1000],s2[1000];

int i,c=0;

printf("Enter string1: ");

gets(s1);

printf("Enter string2: ");

gets(s2);

if(strlen(s1)==strlen(s2))

for(i=0;s2[i]!='\0';i++)

if(s1[i]==s2[i])

c++;

if(c==i)

printf("strings are equal");

else

printf("strings are not equal");

else

printf("strings are not equal");

return 0;
ID: 15
Name:

Output:

Problem 9:

#include<stdio.h>

int main(){

char *str[10], *t;

int i,j;

printf("Enter your word\n");

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

scanf("%s",&str[i]);

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

for(j=i+1; j<5; j++)


ID: 16
Name:

if (strcmp(&str[j-1], &str[j]) > 0)

t=str[j];

str[j]=str[j-1];

str[j-1]=t;

printf("\n");

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

printf("%s\n",&str[i]);

return 0;

Output:
ID: 17
Name:

Problem 10:

#include<stdio.h>

int main(){

int row,cal,n;

char array[10] = {'U', 'N', 'I', 'V', 'E', 'R', 'S', 'I', 'T', 'Y'};

for(row=1;row<=10;row=row+2){

for(cal=0;cal<=row;cal=cal+1){

printf("%c ",array[cal]);

printf("\n");

for(row=10-3;row>=1;row=row-2){

for(cal=0;cal<=row;cal=cal+1){

printf("%c ",array[cal]);

printf("\n");

}
ID: 18
Name:

return 0;

Output :

You might also like