Lab Report 8
Lab Report 8
Lab Report 8
Name:
CSE-102
Computer Programming Laboratory
Student ID
Student Name
Section EA-221
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:
#include <stdio.h>
int main()
scanf("%d",&num1);
scanf("%d",&num2);
scanf("%d",&num3);
p1 = &num1;
p2 = &num2;
ID: 3
Name:
p3 = &num3;
else{
else{
else{
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;
scanf("%d",&size);
scanf("%d",&arr[i]);
sum+=arr[i];
return 0;
}
ID: 5
Name:
Output:
Problem 3:
#include <stdio.h>
int t;
t = *x;
*x = *y;
*y = t;
int main()
int num1,num2;
ID: 6
Name:
scanf("%d",&num1);
scanf("%d",&num2);
swap(&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()
int i;
characters++;
words++;
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;
gets(s);
for(i=0;s[i];i++)
s[i]=s[i+k];
k++;
i--;
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() {
int i;
gets(s1);
i = 0;
s2[i] = s1[i];
i++;
s2[i] = '\0';
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()
printf("Input a string\n");
gets(source);
strcpy(destination, source);
return 0;
Output:
Problem 7:
#include <stdio.h>
#include <string.h>
int main()
char s1[1000],s2[1000];
int i,j;
gets(s1);
gets(s2);
j=strlen(s1);
for(i=0;s2[i]!='\0';i++)
s1[i+j]=s2[i];
s1[i+j]='\0';
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;
gets(s1);
gets(s2);
if(strlen(s1)==strlen(s2))
for(i=0;s2[i]!='\0';i++)
if(s1[i]==s2[i])
c++;
if(c==i)
else
else
return 0;
ID: 15
Name:
Output:
Problem 9:
#include<stdio.h>
int main(){
int i,j;
for(i=0;i<5;i++){
scanf("%s",&str[i]);
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 :