PPS-UNIT-4

SWN | PPS-Practical

Indus University

CSE Section

PPS-Practical

Unit-IV:Array And Strings

1.Write a program to read 10 integers in an array. Find the addition of all elements.

#include <stdio.h>
#include <conio.h>
void main(){
int i,a[10],sum=0;
for(i=0;i<10;i++){
printf("Enter value of array:");
scanf("%d",&a[i]);
sum+=a[i];
}
printf("Sum=%d",sum);
getch();
}

Enter value of array:12
Enter value of array:43
Enter value of array:67
Enter value of array:87
Enter value of array:45
Enter value of array:98
Enter value of array:34
Enter value of array:54
Enter value of array:34
Enter value of array:54
Sum=528

2.Write a program to find the number of odd and even elements from the 1- D array.

#include <stdio.h>
#include <conio.h>
void main(){
int i,a[10],odd=0,even=0;
for(i=0;i<10;i++){
printf("enter number:");
scanf("%d",&a[i]);
if(a[i]%2==0){
even++;
}
else{
odd++;
}
}
printf("There are %d even numbers in the array\n",even);
printf("There are %d odd numbers in the array",odd);
getch();
}

enter number:2
enter number:5
enter number:2
enter number:4
enter number:7
enter number:5
enter number:3
enter number:4
enter number:5
enter number:5
There are 4 even numbers in array
There are 6 odd numbers in array

3.Write a program to reverse the elements of an array and store it in another array.

#include <stdio.h>
#include <conio.h>
void main(){
int i,j,n,x;
printf("Enter the value of n:");
scanf("%d",&n);
int a[n],b[n];
for(i=0;i<n;i++){
printf("Enter value of array:");
scanf("%d",&a[i]);
}
printf("Before reverse:\n");
for(i=0;i<n;i++){
printf("%d\n",a[i]);
}
for(i=0;i<n;i++){
b[i]=a[n-1-i];
}
printf("After reverse:\n");
for(i=0;i<n;i++){
printf("%d\n",b[i]);
}
getch();
}

Enter the value of n:5
Enter value of array:23
Enter value of array:54
Enter value of array:78
Enter value of array:45
Enter value of array:34
Before reverse:
23
54
78
45
34
After reverse:
34
45
78
54
23

4.Write a program to sort elements of array.

#include <stdio.h>
#include <conio.h>
void main(){
int i,j,n,x;
printf("Enter the value of n:");
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++){
printf("Enter value for first array:");
scanf("%d",&a[i]);
}
printf("Before sorting:\n");
for(i=0;i<n;i++){
printf("%d\n",a[i]);
}
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(a[i]>a[j]){
x=a[i];
a[i]=a[j];
a[j]=x;
}
}
}
printf("After sorting:\n");
for(i=0;i<n;i++){
printf("%d\n",a[i]);
}
getch();
}

Enter the value of n:5
Enter value for first array:12
Enter value for first array:54
Enter value for first array:0
Enter value for first array:-23
Enter value for first array:567
Before sorting:
12
54
0
-23
567
After sorting:
-23
0
12
54
567

5.Write a Program to print Addition of two matrices.

#include <stdio.h>
#include <conio.h>
void main(){
int i,j,a[3][3],b[3][3],r[3][3];
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("Enter value of first array:");
scanf("%d",&a[i][j]);
}
}
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("Enter value of second array:");
scanf("%d",&b[i][j]);
}
}
printf("Result:\n");
for(i=0;i<3;i++){
for(j=0;j<3;j++){
r[i][j]=a[i][j]+b[i][j];
printf("%d+%d=%d\n",a[i][j],b[i][j],r[i][j]);
}
}
getch();
}

Enter value of first array:1
Enter value of first array:2
Enter value of first array:3
Enter value of first array:4
Enter value of first array:5
Enter value of first array:6
Enter value of first array:7
Enter value of first array:8
Enter value of first array:9
Enter value of second array:9
Enter value of second array:8
Enter value of second array:7
Enter value of second array:6
Enter value of second array:5
Enter value of second array:4
Enter value of second array:3
Enter value of second array:2
Enter value of second array:1
Result:
1+9=10
2+8=10
3+7=10
4+6=10
5+5=10
6+4=10
7+3=10
8+2=10
9+1=10

6.Program to remove duplicate numbers from a list of numbers and print the list without duplicate numbers.

#include <stdio.h>
#include <conio.h>
void main(){
int i,j,n;
printf("Enter the value of n:");
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++){
printf("Enter number:");
scanf("%d",&a[i]);
}
printf("Before removing dublicate numbers:\n");
for(i=0;i<n;i++){
printf("%d\n",a[i]);
}
for(i=0;i<n;i++){
for(j=i+1;j<n;){
if (a[j] == a[i]) {
for (int k = j; k < n - 1; k++) {
a[k] = a[k + 1];
}
n--;
} else {
j++;
}
}
}
printf("After removing dublicate numbers:\n");
for(i=0;i<n;i++){
printf("%d\n",a[i]);
}
getch();
}

Enter the value of n:5
Enter number:12
Enter number:12
Enter number:45
Enter number:2
Enter number:45
Before removing dublicate numbers:
12
12
45
2
45
After removing dublicate numbers:
12
45
2

7.Write a Program to print Multiplication of two matrices.

#include <stdio.h>
#include <conio.h>
void main(){
int a[3][3],b[3][3],c[3][3],i,j,k,sum;
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("Enter the value of first array:");
scanf("%d",&a[i][j]);
}
}
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("Enter the value of second array:");
scanf("%d",&b[i][j]);
}
}
for(i=0;i<3;i++){
for(j=0;j<3;j++){
sum=0;
for(k=0;k<3;k++){
sum+=a[i][k]*b[k][j];
}
c[i][j]=sum;
}
}
printf("Result:\n");
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("%d\n",c[i][j]);
}
}
getch();
}

Enter the value of first array:1
Enter the value of first array:2
Enter the value of first array:3
Enter the value of first array:4
Enter the value of first array:5
Enter the value of first array:6
Enter the value of first array:7
Enter the value of first array:8
Enter the value of first array:9
Enter the value of second array:9
Enter the value of second array:8
Enter the value of second array:7
Enter the value of second array:6
Enter the value of second array:5
Enter the value of second array:4
Enter the value of second array:3
Enter the value of second array:2
Enter the value of second array:1
Result:
30
24
18
84
69
54
138
114
90

8.Read the marks of five subjects obtained by five students in an examination. Display the top two student’s codes and their marks.

#include <stdio.h>
#include <conio.h>
void main(){
int i,j,codes[5],sub_marks[5][5];
for(i=0;i<5;i++){
printf("Enter student code:");
scanf("%d",&codes[i]);
printf("Enter marks for student %d:\n",i+1);
for(j=0;j<5;j++){
printf("Enter marks for subject %d:",j+1);
scanf("%d",&sub_marks[i][j]);
}
}
int total_marks[5];
for(i=0;i<5;i++){
total_marks[i]=0;
for(j=0;j<5;j++){
total_marks[i]+=sub_marks[i][j];
}
}
for(i=0;i<5;i++){
for(j=i+1;j<5;j++){
if(total_marks[i]<total_marks[j]){
int temp= total_marks[i];
total_marks[i]=total_marks[j];
total_marks[j]=temp;
temp=codes[i];
codes[i]=codes[j];
codes[j]=temp;
}
}
}
printf("\nTop two students:\n");
for(i=0;i<2;i++){
printf("Student code:%d,Total marks:%d\n",codes[i],total_marks[i]);
}
getch();
}

Enter student code:111
Enter marks for student 1:
Enter marks for subject 1:67
Enter marks for subject 2:76
Enter marks for subject 3:89
Enter marks for subject 4:56
Enter marks for subject 5:87
Enter student code:222
Enter marks for student 2:
Enter marks for subject 1:56
Enter marks for subject 2:88
Enter marks for subject 3:99
Enter marks for subject 4:45
Enter marks for subject 5:67
Enter student code:333
Enter marks for student 3:
Enter marks for subject 1:34
Enter marks for subject 2:65
Enter marks for subject 3:72
Enter marks for subject 4:93
Enter marks for subject 5:55
Enter student code:444
Enter marks for student 4:
Enter marks for subject 1:98
Enter marks for subject 2:67
Enter marks for subject 3:99
Enter marks for subject 4:100
Enter marks for subject 5:89
Enter student code:555
Enter marks for student 5:
Enter marks for subject 1:78
Enter marks for subject 2:98
Enter marks for subject 3:45
Enter marks for subject 4:33
Enter marks for subject 5:76

Top two students:
Student code:444,Total marks:453
Student code:111,Total marks:375

9.Write a program to insert an element in an array at specified position.

#include <stdio.h>
#include <conio.h>
void main() {
int array[100], size, position, value;
printf("Enter the size of the array: ");
scanf("%d", &size);
printf("Enter elements of the array:\n");
for (int i = 0; i < size; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &array[i]);
}
printf("Enter the position where you want to insert the element: ");
scanf("%d", &position);
printf("Enter the value to insert: ");
scanf("%d", &value);
for (int i = size; i >= position; i--) {
array[i] = array[i - 1];
}
array[position - 1] = value;
size++;
printf("Array after insertion:\n");
for (int i = 0; i < size; i++) {
printf("%d ", array[i]);
}
printf("\n");
getch();
}

Enter the size of the array: 4
Enter elements of the array:
Element 1: 12
Element 2: 34
Element 3: 54
Element 4: 67
Enter the position where you want to insert the element: 3
Enter the value to insert: 484
Array after insertion:
12 34 484 54 67

10.Write a program to find the length of a string.

#include <stdio.h>
#include <conio.h>
void main(){
char ch[100];
int length=0;
printf("your name:");
printf("Any text:");
fgets(ch,sizeof(ch),stdin);
ch[strcspn(ch, "\n")] = '\0';
while(ch[length]!='\0'){
length++;
}
printf("Length=%d",length);
getch();
}

Any text:hello
Length=5

11.Write a program to reverse the string.(without inbuilt Function)

#include <stdio.h>
#include <conio.h>
#include <string.h>
void main(){
char ch[100],rev[100];
int i,length=0;
printf("Any text:");
fgets(ch, sizeof(ch), stdin);
while(ch[length]!='\0'){
length++;
}
for(i=0;i<length;i++){
rev[i]=ch[length-1-i];
}
rev[length]='\0';
printf("Original text:%s\n",ch);
printf("Reversed text:%s",rev);
getch();
}

Any text:Coding ninja
Original text:Coding ninja
Reversed text:ajnin gnidoC

12.Write a program to convert a string in to lower case and upper case.

#include <stdio.h>
#include <conio.h>
#include <string.h>
void main(){
char ch[100];
int i;
printf("Any text:");
fgets(ch, sizeof(ch), stdin);
printf("Original text:%s",ch);
printf("In lowercase:%s",strlwr(ch));
printf("In uppercase:%s",strupr(ch));
getch();
}

Any text:Hello Friends
Original text:Hello Friends
In lowercase:hello friends
In uppercase:HELLO FRIENDS

13.Write a menu driven program for the implementation of all build-in string functions.

#include <stdio.h>
#include <string.h>
#include <conio.h>
void main() {
char str[100];
int choice;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
do {
printf("\nString Functions Menu:\n");
printf("1. Length of the string\n");
printf("2. Copy the string\n");
printf("3. Concatenate with another string\n");
printf("4. Compare with another string\n");
printf("5. Convert to uppercase\n");
printf("6. Convert to lowercase\n");
printf("7. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
getchar();
switch (choice) {
case 1:
printf("Length of the string: %d\n", strlen(str));
break;
case 2: {
char copy[100];
strcpy(copy, str);
printf("Copied string: %s\n", copy);
break;
}
case 3: {
char anotherStr[100];
printf("Enter another string to concatenate: ");
fgets(anotherStr, sizeof(anotherStr), stdin);
strcat(str, anotherStr);
printf("Concatenated string: %s\n", str);
break;
}
case 4: {
char anotherStr[100];
printf("Enter another string to compare: ");
fgets(anotherStr, sizeof(anotherStr), stdin);
int result = strcmp(str, anotherStr);
if (result == 0)
printf("Both strings are equal.\n");
else if (result < 0)
printf("String 1 is less than string 2.\n");
else
printf("String 1 is greater than string 2.\n");
break;
}
case 5:
printf("String in uppercase: %s\n", strupr(str));
break;
case 6:
printf("String in lowercase: %s\n", strlwr(str));
break;
case 7:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please enter a number between 1 and 8.\n");
}
} while (choice != 7);
getch();
}

Enter a string: jadeja

String Functions Menu:
1. Length of the string
2. Copy the string
3. Concatenate with another string
4. Compare with another string
5. Convert to uppercase
6. Convert to lowercase
7. Exit
Enter your choice: 1
Length of the string: 7

14.Program to extract n characters starting from m in a given string. (String, n and m should be provided as inputs).

#include <stdio.h>
#include <conio.h>
#include <string.h>
void main() {
char str[100];
int n, m;
// Input string
printf("Enter a string: ");
fgets(str,sizeof(str),stdin);
printf("Enter the starting position (m): ");
scanf("%d", &m);
printf("Enter the number of characters to extract (n): ");
scanf("%d", &n);
char extracted[n + 1];
int j = 0;
for (int i = m - 1; i < m - 1 + n; i++) {
extracted[j] = str[i];
j++;
}
extracted[j] = '\0';
printf("Extracted string: %s\n", extracted);
getch();
}

Enter a string: indus university
Enter the starting position (m): 3
Enter the number of characters to extract (n): 9
Extracted string: dus unive

15.Find out occurrence of each character in a given string.

#include <stdio.h>
#include <conio.h>
#include <string.h>
void main() {
char str[100];
int count[256] = {0};
printf("Enter a string: ");
fgets(str,sizeof(str),stdin);
for (int i = 0; i < strlen(str); i++) {
count[(int)str[i]]++;
}
printf("Character\tFrequency\n");
for (int i = 0; i < 256; i++) {
if (count[i] != 0) {
printf("%c\t\t%d\n", i, count[i]);
}
}
getch();
}

Enter a string: djsjldhfh fuuduf dsjfihdhf dhuifhisd
Character            Frequency
                            1 //For character '\0'
                            3 //For spaces
d                          7
f                           6
h                          6
i                           3
j                           3
l                           1
s                           3
u                          4