Lab 8 (C Programming) Solution
Mar 6, 2019programs written here are compiled and run in gcc (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0
1)Write a program to find the sum of all the elements of an array using pointer.
Source Code:
#include <stdio.h> #include <stdlib.h> int main() { float *arr_ptr, sum = 0; int n,i; /* here we use dynamic memory allocation you can use array for fixed number of array size */ printf("Enter the number of elements of an array:"); scanf("%d", &n); arr_ptr = malloc(sizeof(float) * n); printf("Enter array elements:"); /* reading array elements */ for(i = 0; i < n; i++){ scanf("%f", arr_ptr + i); sum += *(arr_ptr + i); } printf("\nSUM = %f\n", sum); return 0; }
Output:
Enter the number of elements of an array:7 Enter array elements:2.3 3.3 2.2 1.1 8 6.9 9 SUM = 32.800003
2)Write a program to swap two variables using pointer and function.
Source Code:
#include <stdio.h> void swap(int *first, int *second){ int temp; temp = *first; *first = *second; *second = temp; } int main(){ int a = 80, b = 100; printf("Before swap (a,b)= (%d, %d)\n", a,b); swap(&a, &b); printf("After swap (a,b)= (%d, %d)\n", a,b); return 0; }
Output:
Before swap (a,b)= (80, 100) After swap (a,b)= (100, 80)
3)Write a program to read the paragraph from the user and count the number of characters, words and lines in that paragraph.
Source Code:
#include <stdio.h> int main() { char paragraph[300], *ch_ptr; int i, c_count = 0, w_count = 0, l_count = 0; printf("Enter the paragraph terminated by dollar($):\n"); scanf("%[^$]", paragraph); for(i = 0; paragraph[i] != '\0'; i++){ if(paragraph[i] == ' ' || paragraph[i] == '\n'){ w_count++; } if(paragraph[i] == '\n'){ l_count++; } c_count++; } printf("Characters = %d\n", c_count); printf("Words = %d\n", w_count + 1); printf("Lines = %d\n", l_count + 1); return 0; }
Output:
Enter the paragraph terminated by dollar($): hello this is me computer programmer writing codes in c programming and this is the program to count character words and lines of paragraph$ Characters = 139 Words = 24 Lines = 3
4)Write a program to read the sentence from the user and delete all the vowels from the sentence.
Source Code:
#include <stdio.h> #include <string.h> int main() { char paragraph[300], *ch_ptr; int i,j , c_count = 0, w_count = 0, l_count = 0; printf("Enter the string:\n"); scanf("%[^\n]", paragraph); for(i = 0; paragraph[i] != '\0'; i++){ if(paragraph[i] == 'a' || paragraph[i] == 'e' || paragraph[i] == 'i' || paragraph[i] == 'o' || paragraph[i] == 'u' || paragraph[i] == 'A' || paragraph[i] == 'E' || paragraph[i] == 'I' || paragraph[i] == 'O' || paragraph[i] == 'U'){ for(j = i; paragraph[j] != '\0'; j++){ paragraph[j] = paragraph[j + 1]; }
/* this is for consequtive occurance of vowels */ i--; } } printf("Refined String : %s\n", paragraph); return 0; }
Output:
Enter the string: hello this is again me, computer programmer to write another program in c Refined String : hll ths s gn m, cmptr prgrmmr t wrt nthr prgrm n c
5)Write a program to copy one string from another string without using string handling functions.
Source Code:
#include <stdio.h> int main() { char str1[50], str2[50]; int i; printf("Enter the first string:\n"); scanf(" %[^\n]", str1); printf("Enter the Second string:\n"); scanf(" %[^\n]", str2); for(i = 0; str2[i] != '\0';i++){ str1[i] = str2[i]; } str1[i] = '\0'; printf("Copied String: %s\n", str1); return 0; }
Output:
Enter the first string: hello kathmandu Enter the Second string: namaste nepal Copied String: namaste nepal
6)Write a program to concatenate two strings.
Source Code:
#include <stdio.h> /* function to get the string length */ int find_length(char *str){ int i; for(i = 0; *(str + i) != '\0'; i++){ } return i; } int main() { char str1[50], str2[50]; int i, length1; printf("Enter the first string:\n"); scanf(" %[^\n]", str1); printf("Enter the Second string:\n"); scanf(" %[^\n]", str2); length1 = find_length(str1); for(i = 0; str2[i] != '\0';i++){ str1[length1 + i] = str2[i]; } str1[length1 + i] = '\0'; printf("Concatinated String: %s", str1); return 0; }
Output:
Enter the first string: hello from Enter the Second string: nepal Concatinated String: hello from nepal
7)Write a program to compare two strings entered by the user
Source Code:
#include <stdio.h> /* function to get the string length */ int find_length(char *str){ int i; for(i = 0; *(str + i) != '\0'; i++){ } return i; } /* function that copares two strings */ int compare_string(char *str1, char *str2){ int i, l1, l2; l1 = find_length(str1); l2 = find_length(str2); for(i = 0; *(str1+i) != '\0' || *(str2 + i) != '\0'; i++){ if(*(str1+i) > *(str2+i)){ return 1; }else if(*(str1+i) < *(str2+i)){ return -1; } } if(l1 == l2){ return 0; }else{ return -1; } } int main() { char str1[50], str2[50]; int state; printf("Enter the first string:\n"); scanf(" %[^\n]", str1); printf("Enter the Second string:\n"); scanf(" %[^\n]", str2); state = compare_string(str1, str2); if(state == 1){ printf("First one is greater."); }else if(state == -1){ printf("Second one is greater."); }else{ printf("They are same."); } return 0; }
Output:
Enter the first string: computer programmer Enter the Second string: computer engineer First one is greater.
8)Write a program to compare two strings entered by the user
Source Code:
#include <stdio.h> #include <stdlib.h> #define N 10 /* function to get the string length */ int find_length(char *str){ int i; for(i = 0; *(str + i) != '\0'; i++){ } return i; } /* function that copares two strings */ int compare_string(char *str1, char *str2){ int i, l1, l2; l1 = find_length(str1); l2 = find_length(str2); for(i = 0; *(str1+i) != '\0' || *(str2 + i) != '\0'; i++){ if(*(str1+i) > *(str2+i)){ return 1; }else if(*(str1+i) < *(str2+i)){ return -1; } } if(l1 == l2){ return 0; }else{ return -1; } } void swapp_string(char *str1, char *str2){ char temp_ch; int i, len1 = find_length(str1), len2 = find_length(str2), largest_str; largest_str = (len1 > len2) ? len1:len2; for(i = 0; i < largest_str; i++){ temp_ch = *(str1 + i); *(str1 + i) = *(str2 + i); *(str2 + i) = temp_ch; } } int main() { char *name[N]; int i, j; /* Allocating 40 byte memory for each element of array*/ for(i = 0; i < N; i++){ name[i] = malloc(sizeof(char) * 40); } /* read the names*/ for(i = 0; i < N; i++){ printf("Enter name : %d:", i+1); scanf(" %[^\n]", name[i]); } /* sorting the names */ for(i = 0; i < N - 1; i++){ for(j = i + 1; j < N; j++){ if(compare_string(name[i], name[j]) == 1){ swapp_string(name[i], name[j]); } } } printf("SORTED STRING:\n\n"); for(i = 0; i < N; i++){ printf("%d: %s\n", i+1, name[i]); } return 0; }
Output:
Enter name : 1:ram bahadur karki Enter name : 2:rita timilsina Enter name : 3:aarati bhatta Enter name : 4:aarati bhantana Enter name : 5:ramhari bhurtel Enter name : 6:rishiram tiwari Enter name : 7:haribansha achcharya Enter name : 8:nepali babu Enter name : 9:harkha ram Enter name : 10:sitaram kattel SORTED STRING: 1: aarati bhantana 2: aarati bhatta 3: haribansha achcharya 4: harkha ram 5: nepali babu 6: ram bahadur karki 7: ramhari bhurtel 8: rishiram tiwari 9: rita timilsina 10: sitaram kattel
9)Write a program to print the following pattern.
PROGRAMMING
PROGRAMMIN
PROGRAMMI
PROGRAMM
PROGRAM
PROGRA
PROGR
PROG
PRO
PR
P
Source Code:
#include <stdio.h> int main() { char string[] = "PROGRAMMING"; int i,j, length; for(i = 0; string[i] != '\0';i++){ } length = i; for(i = 0; i < length; i++){ for(j = 0; j < length - i; j++){ printf("%c", string[j]); } printf("\n"); } return 0; }
Output:
PROGRAMMING PROGRAMMIN PROGRAMMI PROGRAMM PROGRAM PROGRA PROGR PROG PRO PR P