0% found this document useful (0 votes)
15 views

lab 6

This lab report from Triton International College focuses on string manipulation in C programming. It includes multiple C program examples demonstrating string input, length calculation, reversal, case conversion, copying, comparison, concatenation, palindrome checking, sorting, and searching. The conclusion emphasizes the importance of understanding string arrays for efficient algorithm development and program management.

Uploaded by

sssssbijaya987
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

lab 6

This lab report from Triton International College focuses on string manipulation in C programming. It includes multiple C program examples demonstrating string input, length calculation, reversal, case conversion, copying, comparison, concatenation, palindrome checking, sorting, and searching. The conclusion emphasizes the importance of understanding string arrays for efficient algorithm development and program management.

Uploaded by

sssssbijaya987
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

TRITON INTERNATIONAL COLLEGE

KATHMANDU, NEPAL

Lab Report Of Computer Science on C Programming

Lab Report No: 05

SUBMITTED BY : SUBMITTED TO:

NAME: PUJAN KHIUNJU DEPARTMENT OF COMPUTER

ROLL NO: 12728672 TRITON INTERNATIONAL COLLEGE

GRADE: 11 KATHMANDU, NEPAL

SECTION: P505

Date of Submission: 2081/11/18


Lab-06 [String]
A string is defined as an array of characters which contains Characters,
symbols, numbers etc. It is the set of character digits and symbol.
In C, a string array is used to store multiple strings and can be implemented as a
2D character array or an array of string pointers. A 2D character array (char
names[3][10] = {"Alice", "Bob", "Charlie"};) stores strings in fixed-size rows,
while an array of pointers (char *cities[] = {"New York", "London", "Tokyo"};)
is more memory-efficient for variable-length strings. Strings in C end with a
null character (\0), and functions from like strcpy(), strlen(), and strcmp() help
manipulate them. 2D arrays are useful when string sizes are fixed, while pointer
arrays are preferred for dynamic strings
Syntax
char string name [string-s120];
Examples
char name [10]; // Array of characters with size to
char address [100],// Array of characters with size 10
char str [100]; // Array of characters with size 100
1. Write C Program to input any five strings and print them.
#include <stdio.h>
int main() {
int i,j;
char str[5][100];
printf("Enter 5 strings:\n");
for ( i = 0; i < 5; i++) {
printf("Enter string %d: ", i + 1);
gets(str[i]);
}
printf("\nThe strings you entered are:\n");
for ( i = 0; i < 5; i++) {
printf("String %d: %s", i + 1, str[i]);
}
return 0;
}
2. Write C Program to find length of entered string using strlen().
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
printf("Enter a string: ");
gets(str);
printf("The length of the entered string is: %d\n", strlen(str));
return 0;
}

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.

You might also like