Assignment 7 PPS
Problem 1: WAP in C to find length of a string.
sol.
#include <stdio.h>
#include <string.h>
int main() {
char A[50];
printf("Enter Your String\n");
gets(A);
int length = strlen(A);
printf("The length of the String is: %d\n", length);
return 0;
}
Problem 2: WAP in C to concatenate two strings.
sol.
#include <stdio.h>
#include <string.h>
int main() {
char A[50],B[50];
printf("Enter the first string: ");
gets(A);
printf("Enter the second string: ");
gets(B);
strcat(A,B);
printf("Concatenated string: %s\n",A);
return 0;
}
Problem 3: WAP in C to compare two strings.
sol.
#include <stdio.h>
#include <string.h>
int main() {
char A[50], B[50];
printf("Enter the first string: ");
gets(A);
printf("Enter the second string: ");
gets(B);
int result = strcmp(A, B);
if (result == 0) {
printf("The strings are equal.\n");
} else if (result < 0) {
printf("The first string is smaller than the second string.\n");
} else {
printf("The first string is larger than the second string.\n");
}
return 0;
}
Problem 4: WAP in C to convert lowercase string to uppercase.
sol.
#include <stdio.h>
#include <string.h>
int main( ) {
char s[1000];
printf("Enter a string: ");
gets(s);
strupr(s);
printf("Uppercase string: %s\n", s);
return 0;
}
Problem 5: WAP in C to find total number of alphabets, digits or special character in a string.
sol.
#include <stdio.h>
int main() {
char str[1000];
int i, alphabets, digits, special;
alphabets = digits = special = 0;
printf("Enter a string: ");
gets(str);
for(i = 0; str[i]; i++) {
if ((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z')) {
alphabets++;
} else if (str[i] >= '0' && str[i] <= '9') {
digits++;
} else {
special++;
}
}
printf("Total number of alphabets = %d\n", alphabets);
printf("Total number of digits = %d\n", digits);
printf("Total number of special characters = %d\n", special);
return 0;
}
Problem 6: WAP in C to count total number of words in a string.
sol.
#include<stdio.h>
int main() {
char str[1000];
int i,count = 0;
printf("Enter a string: ");
gets(str);
for (i = 0; str[i] != '\0'; i++) {
if (str[i] == ' ') // Check for spaces to count words
{
count++;
}
}
count++; // Add 1 to the count to account for the last word
printf("Total number of words: %d\n", count);
return 0;
}
Problem 7: WAP in C to check whether a string is palindrome or not.
sol.
#include<stdio.h>
int main() {
char s[50], r[50];
int i, j, t = 0;
printf("Enter a string: ");
gets(s);
for (i = 0; s[i] != '\0'; i++);
for (i = i - 1, j = 0; i >= 0; i--, j++) {
r[j] = s[i];
}
r[j] = '\0';
for (i = 0; s[i] != '\0'; i++) {
if (s[i] != r[i]) {
t = 1;
break;
}
}
if (t == 1)
printf("The Given String is Not Palindrome");
else
printf("The Given String is Palindrome");
return 0;
}
Problem 10: WAP in C to create a structure/union Student with three members,name: string, roll_no: integer, marks[]: array of float (4 subjects).The program should take information of 05 students from the user and store it in the array of structure/union, compute total marks and percentage obtained by each student and display the information on the screen.
sol.
#include <stdio.h>
// Define structure Student
struct Student {
char name[50];
int roll_no;
float marks[4];
};
int main() {
// Declaration of an array of structure Student
struct Student students[5];
int i,j;
// Inputing information for 5 students from user
for (i = 0; i < 5; i++) {
printf("Enter details for student %d:\n", i + 1);
printf("Name: ");
scanf("%s", students[i].name);
printf("Roll No: ");
scanf("%d", &students[i].roll_no);
printf("Enter marks for 4 subjects:\n");
for (j = 0; j < 4; j++) {
printf("Subject %d: ", j + 1);
scanf("%f", &students[i].marks[j]);
}
printf("\n");
}
// Calculation of total marks and percentage for each student and display information.
printf("\nStudent Information:\n");
for (i = 0; i < 5; i++) {
float totalMarks = 0;
for (j = 0; j < 4; j++) {
totalMarks += students[i].marks[j];
}
float percentage = (totalMarks / 4.0);
printf("Student %d:\n", i + 1);
printf("Name: %s\n", students[i].name);
printf("Roll No: %d\n", students[i].roll_no);
printf("Total Marks: %.2f\n", totalMarks);
printf("Percentage: %.2f%%\n\n", percentage);
}
return 0;
}
0 comments:
Post a Comment