0% found this document useful (0 votes)
15 views4 pages

C Programming: String Functions Guide

The document outlines an assignment for first-year engineering students at K. K. Wagh Institute, focusing on string operations in C programming. It includes objectives, prerequisites, theory on string handling, and details about various string library functions such as strlen(), strcpy(), and strcat(). Students are required to implement a menu-driven program to perform specific string operations using both library and user-defined functions.

Uploaded by

sjdkpatil
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)
15 views4 pages

C Programming: String Functions Guide

The document outlines an assignment for first-year engineering students at K. K. Wagh Institute, focusing on string operations in C programming. It includes objectives, prerequisites, theory on string handling, and details about various string library functions such as strlen(), strcpy(), and strcat(). Students are required to implement a menu-driven program to perform specific string operations using both library and user-defined functions.

Uploaded by

sjdkpatil
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

K. K.

Wagh Institute of Engineering Education and Research, Nashik


(Autonomous from Academic Year 2022-23)
F. Y. B. Tech.
Pattern 2022 Semester: I
FYE221010: Programming in C

Assignment No. 04
Title: Understand and implement library functions of string
Problem Statement:
Draw a flowchart/write an algorithm / a pseudo-code and write a menudriven C program to perform
following string operations using library and user defined function:
i.​ Find length of a string
ii.​ Copy a string
iii.​ Concatenate the string
iv.​ Compare two strings
v.​ Convert to Uppercase and Lowercase
Prerequisites:
Hardware Requirement: Desktop Computer / laptop computer.
Software Requirement: Linux Operating System with GCC
Objectives:
1.​ To get familiar with library functions of string.
2.​ To learn syntax and working of string functions.

Theory:
Strings Handling
A collection of characters is called as string. In the absence of string data type to store strings in C we have to
simulate string using an array of characters. Any group of characters (except double quote sign)
defined between double quotation marks is a constant string. Character strings are often used to build
meaningful and readable programs. The common operations performed on character strings are:
●​ Reading and writing strings
●​ Combining strings together
●​ Copying one string to another
●​ Comparing strings for equality
●​ Extracting a portion of a string

Declaring a String
An array of characters is also a collection of characters. So an array of characters is used
to represent a string in C. For example, to store a name, which may be up to 50
characters, you would declare a string as follows:
●​ name can contain up to 50 characters
char name[50];
●​ to store 10 names where each name can contain 20 characters
char names[10][20];

How String is stored?


A string is stored as an array in the memory. However, when you declare a string of 20
characters, it doesn’t always contain 20 characters. It may contain only 15 characters. So
to identify the end of the actual characters in a string C is storing a null character (with
ASCII code 0) at the end of the string.
Null character is written as ‘\0’. It is the character for ASCII code 0. As no other character
contains ASCII code 0, when we encounter a null character in a string we can stop taking characters after that
character.
​ ​ Input/output of Strings
You can read a string from keyboard. C has provided a conversion character %s forinputting and
outputting string. You can read a string using scanf() & print using printf().The following code
snippet is to read name of the user and display the same.
char name[30];
printf("What’s your name: ");
scanf("%s",name);
printf("Welcome %s",name);
There are few important points that you have to understand about reading strings using scanf()
●​ You must not precede string variable with & (ampersand). The reason will be evident to
you once you understand pointers. Giving & before the string variable is a logical error.
Compiler may not complain about it but program will not work.
●​ You can read the data from keyboard only up to first whitespace character (space or tab).
That means if I want to enter my name, I can enter only Johson and not complete name
“Johnson P D”. This is because of the fact that scanf() function assumes the end of the
input for a field once it encounters a whitespace character.
Note: While using %s to read a string with scanf(), do not precede variable with & symbol.
Another important point about strings is, all standard functions such as scanf() andprintf() take care of null
character at the end of the [Link] instance, when you enter a string using scanf(), the function will
automatically put anull at the end of the string. And in the same way while you are printing, printf()
willtake characters until null character is encountered.

String I/O
In order to read and write string of characters the functions gets() and puts() are usedgets() function
reads the string and puts() function takes the string as argument and writeson the screen.

The gets() and puts() functions offer simple alternatives with regard to the use of scanf()and printf()
which are of reading and displaying strings respectively. These executes fasterthan printf() and
scanf() due to the non-use of format specifies and occupies less space.
String “string.h” Library
C provides a set of functions that performs operations on strings. These functions take astring and take actions
such as reversing content of string, converting the string to uppercase and return length of the string.
The following are functions from string library. Allfunctions are declared in string.h header file.

strlen() function
This function counts and returns the number of characters in a string. The length does notinclude a
null character.
Syntax: len = strlen(string);
where len is integer variable which receives the value of lengthof the string.

strlwr() function
This function converts all characters in a string from uppercase to lowercase.
Syntax: strlwr(string);
For Example: strlwr("EXFORSYS");
converts to exforsys.

strupr() function
This function converts all characters in a string from lowercase to uppercase.
Syntax: strupr(string);
For Example: strupr("exforsys");
converts to EXFORSYS.

strrev() function
This function reverses the characters in a string.
Syntax: strrev(string);
For Example: strrev("exforsys");
reverses the characters to sysrofxe.

strcpy() function
C does not allow to assign the characters to a string directly as in the statement
name="exforsys"; instead use the strcpy() function found in most compilers.
Syntax: strcpy(string1,string2);
For Example: strcpy(name,"exforsys");
String exforsys is assigned to the string called name.

strcmp() function
In C we cannot directly compare the value of 2 strings in a condition likeif(string1==string2)
Most libraries however contain the strcmp() function, which returns a zero if 2 strings areequal, or a
non zero number (>0 or <0) if the strings are not same.
Syntax: strcmp(string1,string2);
string1 and string2 may be variables or constants. Some computersreturn a negative value if string1 is
alphabetically less than thesecond and a positive if the string1 is greater than the second.
For Example:
strcmp("their","there");
will return -9 which is the numeric difference betweenASCII 'i' and ASCII 'r'.
strcmp("the","The");
will return 32 which is the numeric difference betweenASCII 't' and ASCII 'T'.
strcmp("hello","hello");
will return 0 as two strings are equal.

strcat() function
When you combine two strings, you add the characters of one string to the end of otherstring. This
process is called concatenation. The strcat() function joins 2 strings together. Ittakes the following
form:
Syntax: strcat(string1,string2);
When the function strcat is executed string2 is appended to string1, the string atstring 2remains
unchanged.
For Example:
strcat(st1,"hello ");
strcat(st2,"world");
printf("%s",strcat(st1,st2));
From the above program segment of value of st1 becomes "hello world".The string at st2 remains
unchanged as "world".

Algorithm/Psuedocode:

Flowchart:

Test case:

Discussion and Conclusion:

You might also like