Untitled
10. Write functions to implement string operations such as copy, compare and concatenate using user defined functions #include<stdio.h> int Length(char s1[]); int compare(char s1[], char s2[]); void concat(char s1[], char s2[]); int main() { char s1[200], s2[100]; int len,res,count; printf("\nEnter the String s1: "); gets(s1); printf("\nEnter String s2 :"); gets(s2); len = Length(s1); printf("\nLength of the String s1 is : %d", len); res = compare(s1, s2); if(res == 0) { printf("\nBoth the Strings are Equal\n"); } else { printf("\nThe Strings are not Equivalent\n"); } concat(s1, s2); printf("\nConcated string is :%s", s1); return(0); } int Length(char s1[]) { int len = 0; while (s1[len] != '\0') len++; return (len); } int compare(char s1[], char s2[]) { int count = 0; while(s1[count] == s2[count]) { if(s1[count] == '\0' || s2[count] == '\0') break; else count++; } if(s1[count] == '\0' && s2[count] == '\0') return 0; else return -1; } void concat(char s1[], char s2[]) { int i, j; i = strlen(s1); for (j = 0; s2[j] != '\0'; i++, j++) { s1[i] = s2[j]; } s1[i] = '\0'; } 11. Write functions to implement string operations such as compare, reverse and find string length. Use the parameter passing techniques. #include <stdio.h> #include <string.h> int compare_strings(char str1[], char str2[]) { int i = 0; while (str1[i] == str2[i] && str1[i] != '\0' && str2[i] != '\0') { i++; } return str1[i] - str2[i]; // Returns 0 for equal, negative for str1 < str2, positive for str1 > str2 } Void reverse_string(char str[]) { int len = strlen(str); for (int i = 0; i < len / 2; i++) { char temp = str[i]; str[i] = str[len - i - 1]; str[len - i - 1] = temp; } } int find_string_length(char str[]) { int len = 0; while (str[len] != '\0') { len++; } return len; } int main() { char str1[50], str2[50]; printf("Enter the first string: "); fgets(str1, 50, stdin); // Use fgets to handle spaces in input str1[strcspn(str1, "\n")] = '\0'; // Remove trailing newline printf("Enter the second string: "); fgets(str2, 50, stdin); str2[strcspn(str2, "\n")] = '\0'; int comparison = compare_strings(str1, str2); if (comparison == 0) { printf("The strings are equal.\n"); } else if (comparison < 0) { printf("The first string is lexicographically smaller.\n"); } else { printf("The second string is lexicographically smaller.\n"); } reverse_string(str1); printf("Reversed first string: %s\n", str1); int length = find_string_length(str2); printf("Length of second string: %d\n", length); return 0; } 12. Write a program to convert given binary to decimal using function. #include <stdio.h> int convert(int); int main() { int dec, bin; printf("Enter a binary number: "); scanf("%d", &bin); dec = convert(bin); printf("The decimal equivalent of %d is %d.\n", bin,dec); return 0; } int convert(int bin) { if (bin == 0) { return 0; } else { return (bin % 10 + 2 * convert(bin / 10)); } 13. Implement structures to read, write and compute average marks of the students, list the students scoring above and below the average marks for a class of N students. #include <stdio.h> struct student { char usn[50]; char name[50]; int marks; } s[10]; void main() { int i,n,countav=0,countbv=0; float sum,average; printf("Enter number of Students\n"); scanf("%d",&n); printf("Enter information of students:\n"); for(i=0; i<n;i++) { printf("Enter USN: "); scanf("%s",s[i].usn); printf("Enter name: "); scanf("%s",s[i].name); printf("Enter marks: "); scanf("%d",&s[i].marks); printf("\n"); } for(i=0;i<n;i++) { sum=sum+s[i].marks; } average=sum/n; printf("\nAverage marks: %f",average); countav=0; countbv=0; for(i=0;i<n;i++) { if(s[i].marks>=average) countav++; else countbv++; } printf("\nTotal No of students above average= %d",countav); printf("\nTotal No of students below average= %d",countbv); } 14. Develop a program using pointers to compute the sum mean and standard deviation of all elements stored in an array of N real numbers. #include<stdio.h> #include <conio.h> #include <math.h> int main() { float a[10], *ptr, mean, std, sum=0, sumstd=0; int n,i; printf("Enter the no of elements\n"); scanf("%d",&n); printf("Enter the array elements\n"); for(i=0;i<n;i++) { scanf("%f",&a[i]); } ptr=a; for(i=0;I <n;i++) { sum=sum+ *ptr; ptr++; } mean=sum/n; ptr=a; for(i=0;I <n;i++) { sumstd=sumstd + pow((*ptr - mean),2); ptr++; } std= sqrt(sumstd/n); printf("Sum=%.3f\t",sum); printf("Mean=%.3f\t",mean); printf("Standard deviation=%.3f\t",std); return 0; } 15. Write a c program to copy a text file to another , reading both the input file name and target file name.
Leave a Comment