lab 6
lab 6
KATHMANDU, NEPAL
SECTION: P505
3. Write C Program to read a string with space and reverse that using
strrev()
#include<stdio.h>
#include <string.h>
int main() {
char str[100];
printf("Enter a string: ");
gets(str);
printf("Reversed string: %s\n", strrev(str));
return 0;
}
4. Write C Program to read a string and convert that into lower case and
upper case. [use strlwr() and strupr()]
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
printf("Enter a string: ");
gets(str);
printf("String in lowercase: %s\n", strlwr(str));
printf("String in uppercase: %s\n", strupr(str));
return 0;
}
5. Write C Program to copy a string into another using strcpy().
#include<stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100];
printf("Enter a string: ");
gets(str1);
strcpy(str2,str1);
printf("Original string: %s\n", str1);
printf("Copied string: %s\n", str2);
return 0;
}
6. Input any two strings and know which has greater ASCII value.[Note:
use strcmp()]
#include<stdio.h>
#include<string.h>
int main() {
char str1[100], str2[100];
printf("Enter the first string: ");
gets(str1);
printf("Enter the second string: ");
gets(str2);
int result = strcmp(str1, str2);
if (result > 0) {
printf("The first string has a greater ASCII value.\n"); }
else if (result < 0) {
printf("The second string has a greater ASCII value.\n"); }
else { printf("Both strings have equal ASCII values.\n"); }
return 0;
}
7. Write C Program to read a string and concatenate with another string
using strcat().
#include <stdio.h>
#include<string.h>
int main() {
char str1[100], str2[100];
printf("Enter the first string: ");
gets(str1);
printf("Enter the second string: ");
gets(str2);
strcat(str1, str2);
printf("Concatenated string: %s\n",str1);
return 0;
}
8. Write C Program to know whether a string is palindrome or not.
#include <stdio.h>
#include<string.h>
int main() {
char str[100], reversedStr[100];
int length, i, j;
printf("Enter a string: ");
gets(str);
length = strlen(str);
for (i = 0, j = length - 1; j >= 0; i++, j--) {
reversedStr[i] = str[j];
}
if (strcmp(str, reversedStr) == 0) {
printf("The string is a palindrome.\n"); }
else {
printf("The string is not a palindrome.\n"); }
return 0;
}
9. Write C Program to input any ten names of students and arrange them
in descending/ascending order.
#include <stdio.h>
#include <string.h>
int main() {
char str[50][30], temp[30];
int i, j;
for (i = 0; i < 10; i++) {
printf("\nEnter string %d: ", i + 1);
scanf("%s", str[i]);
}
for (i = 0; i < 10 - 1; i++) {
for (j = i + 1; j < 10; j++) {
if (strcmp(str[i], str[j]) > 0) {
strcpy(temp, str[i]);
strcpy(str[i], str[j]);
strcpy(str[j], temp);
}
}
}
printf("\nThe sorted strings are:\n");
for (i = 0; i < 10; i++) {
printf("%s\n", str[i]);
}
return 0;
}
10. Write C Program to input any ten strings and search a string in that
list.
#include <stdio.h>
#include <string.h>
int main() {
char str[10][100], searchStr[100];
int i,found = 0;
printf("Enter 10 strings:\n");
for ( i = 0; i < 10; i++) {
printf("Enter string %d: ", i + 1);
gets(stri[i]);
}
printf("\nEnter a string to search: ");
gets(searchStr);
for ( i = 0; i < 10; i++) {
if (strcmp(strings[i], searchStr) == 0) {
printf("String found at position %d\n", i + 1);
found = 1;
break;
}
}
if (!found) {
printf("String not
found in the list.\n");
}
return 0;
}
Conclusion:
Understanding string array is essential in developing efficient algorithms and
solving complex problems in programming. By mastering these concepts, you
can gain better control over the flow of your programs, making them more
effective and easier to manage.