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

shivam LAB 6 (1)

The document provides an introduction to strings in C, detailing string handling functions such as strlen(), strrev(), strcpy(), strcat(), strcmp(), strlwr(), and strupr(). It includes example C programs demonstrating how to input, manipulate, and manage strings, including finding string lengths, reversing strings, copying, concatenating, and sorting. The conclusion emphasizes the utility of arrays of strings for efficient data management in programming.

Uploaded by

shivam9746583742
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

shivam LAB 6 (1)

The document provides an introduction to strings in C, detailing string handling functions such as strlen(), strrev(), strcpy(), strcat(), strcmp(), strlwr(), and strupr(). It includes example C programs demonstrating how to input, manipulate, and manage strings, including finding string lengths, reversing strings, copying, concatenating, and sorting. The conclusion emphasizes the utility of arrays of strings for efficient data management in programming.

Uploaded by

shivam9746583742
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

LAB-06 [STRINGS]:

Introduction to string:
String is the set of characters, digits and symbol. The end of string is marked with special character, the ‘\0’ that
means Null character. The size in a character string represents the maximum number of characters that the string
can hold.

Syntax: char string _ name[string _ size];

String Handling Function:


1. strlen() Function:
The strlen() function returns the length of string which takes the string name as an argument.
Syntax: 1= strlen(str);

2. strrev() Function:
The strrev() function is used to reverse the given string. It takes a string, reverses it and stores in the
same string.
Syntax: strrev (str);

3. strcpy() Function:
The strcpy() function is used to copy one string into another string. It takes two strings one empty string
and one given string. It copies the given string into empty string.
Syntax: strcpy[str1,str2];

4. strcat() Function:
The strcat() function is used to join or concatenate one string into other string. It takes two string and
second string is concatenated over first string.
Syntax: strcat[str1,str2];

5. strcmp() Function:
The strcmp() function is used to compare two strings, character by character and stop comparison when
there is a difference in the ASCII value or the end of any one string and returns ASCII difference of the
characters that is integer which is either zero, negative value or positive value.
Syntax: v= strcmp[str1,str2];

6. strlwr() Function:
The strlwr() function is used to convert upper case characters of string into lower case characters.
Syntax: strlwr (str);

7. strupr() Function:
The strupr() function is used to convert lower case characters of string into upper case characters.
Syntax: strupr (str);
1. Write C Program to input any five strings and print them:
#include<stdio.h>

#include<string.h>

int main()

char strings[5][100];

printf("Enter 5 strings:\n");

for (int i = 0; i < 5; i++)

printf("String %d: ", i + 1);

scanf("%s", strings[i]);

printf("\nYou entered:\n");

for (int i = 0; i < 5; i++)

printf("%s\n", strings[i]);

return 0;

OUTPUT:
2. Write C Program to find length of entered string using strlen():
#include<stdio.h>

#include<string.h>

int main() {

char str[50];

int a;

printf("Enter the String:");

gets(str);

a=strlen(str);

printf("Length of string is:%d",a);

OUTPUT:

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("entered string is:");

gets(str);

strrev(str);

printf("Reverse of the given string is:%s",str);

return 0;

OUTPUT:
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("entered string is:");

gets(str);

strlwr(str);

strupr(str);

printf("\nThe string in lowercase is:%s",strlwr(str));

printf("\nThe string in uppercase is:%s",strupr(str));

return 0;

OUTPUT:

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);

puts(str2);

return 0;

OUTPUT:
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

first string: ");

scanf("%s", str1);

printf("Enter second

string: "); scanf("%s",

str2);

int result = strcmp(str1,

str2); if (result > 0) {

printf("'%s' has a greater ASCII value than '%s'.\n", str1, str2);


}

else if (result < 0) {

printf("'%s' has a greater ASCII value than '%s'.\n", str2, str1);


}

else {

printf("Both strings are equal.\n");

return 0;

}
OUTPUT:
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[200], str2[100];

printf("Enter first string: ");

scanf("%s", str1);

printf("Enter second string: ");

scanf("%s", str2);

strcat(str1, str2);

printf("Concatenated string: %s\n", str1);

return 0;

OUTPUT:

8. Write C Program to know whether a string is palindrome or not:


#include <stdio.h>

#include <string.h>

int main() {

char str[100], rev[100];

printf("Enter a string: ");

scanf("%s", str);

strcpy(rev, str);

strrev(rev);

if (strcmp(str, rev) == 0) {

printf("The string '%s' is a palindrome.\n", str);

} else {

printf("The string '%s' is NOT a palindrome.\n", str);

return 0;

OUTPUT:
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 names[10][50], temp[50];
int i, j, order;

printf("Enter 10 student names:\n");


for (i = 0; i < 10; i++) {
printf("Name %d: ", i + 1);
scanf("%s", names[i]);
}

printf("\nChoose sorting order:\n");


printf("1. Ascending Order\n");
printf("2. Descending Order\n");
printf("Enter your choice (1 or 2): ");
scanf("%d", &order);

for (i = 0; i < 9; i++) {


for (j = i + 1; j < 10; j++) {
if ((order == 1 && strcmp(names[i], names[j]) > 0) ||
(order == 2 && strcmp(names[i], names[j]) < 0)) {
strcpy(temp, names[i]);
strcpy(names[i], names[j]);
strcpy(names[j], temp);
}
}
}
printf("\nSorted Names:\n");
for (i = 0; i < 10; i++) {
printf("%s\n", names[i]);
}

return 0;
}

Output:
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 strings[10][50], search[50];

int i, found = 0;

printf("Enter 10 strings:\n");

for (i = 0; i < 10; i++) {

printf("String %d: ", i + 1);

scanf("%s", strings[i]);

printf("\nEnter the string to search: ");

scanf("%s", search);

for (i = 0; i < 10; i++) {

if (strcmp(strings[i], search) == 0) {

found = 1;

break;

} if (found) {

printf("\n'%s' is found in the list at position %d.\n", search, i + 1);

} else {

printf("\n'%s' is not found in the list.\n", search);

return 0;

OUTPUT:

CONCLUSION:

An array of strings in C is useful for storing and managing multiple strings efficiently. Instead of
using multiple char arrays, we can store a list of strings in a single 2D char array. This makes
operations like input handling, searching, sorting, and comparison easier. It is widely used for tasks
like storing names, words, or other text-based data in applications. Arrays of strings simplify data
management and improve code readability in programs that involve handling multiple text entries.

You might also like