1. Lectures on Busy Bee Workshop 1
Busy Bee Workshop – Session VIBusy Bee Workshop – Session VI
This session Outline
String
String Manipulation Functions
String Programs
2. Lectures on Busy Bee Workshop 2
String in CString in C
Sequence of characters is called string.
In C, a string constant is a sequence of
characters enclosed in double quotes.
Examples:
“C Programming”
“SVN College”
“3/390 Nehru Street”
“45”
3. Lectures on Busy Bee Workshop 3
String in C - ImplementationString in C - Implementation
• C implements the stringstring data structure
using arrays of type char.
• We have already used the string
extensively.
– printf(“This program is terminated!n”);
– #define ERR_Message “Error!!”
• Since stringstring is an array, the declaration of
a string is the same as declaring a char
array.
– char string_var[30];
– char string_var[20] = “Initial value”;
4. Lectures on Busy Bee Workshop 4
String in C – Memory StorageString in C – Memory Storage
• The string is always ended with a nullnull
charactercharacter ‘0’‘0’.
• The characters after the null character are
ignored.
• e.g., char str[20] = “Initial value”;
n i t i a l v a l u e ? ? …I 00
[0] [13]
5. Lectures on Busy Bee Workshop 5
String in C – Arrays of StringsString in C – Arrays of Strings
• An array of strings is a two-dimensional
array of characters in which each row is one
string.
– char names[People][Length];
– char names[10][25];
– char month[5][10] = {“January”,
“February”, “March”, “April”, “May”};
6. Lectures on Busy Bee Workshop 6
String in C – Character vs. StringString in C – Character vs. String
7. Lectures on Busy Bee Workshop 7
String in C – Input/OutputString in C – Input/Output
• The placeholder %s%s is used to represent
string arguments in printf and scanf.
• Example:
char message1[12] = "Hello world";
printf(“%s”,message1);
message1:
char message2[12];
scanf(“%s”,message2); // type "Hello" as input
message2:
H e l l o w o r l d 0
H e l l o 0 ? ? ? ? ? ?
8. Lectures on Busy Bee Workshop 8
String in C – Right and Left Justification of StringsString in C – Right and Left Justification of Strings
• The string can be right-justified by placing a
positive number in the placeholder.
– printf(“%8s%8s”, str);
• The string can be left-justified by placing a
negative number in the placeholder.
– Printf(“%-8s%-8s”, str);
9. Lectures on Busy Bee Workshop 9
String in C – Example programString in C – Example program
10. Lectures on Busy Bee Workshop 10
String in C – Library FunctionsString in C – Library Functions
• The string can not be copied by the
assignment operator ‘=’.
– e..g, “str = “Test String”” is not valid.
• C provides string manipulating
functions in the “string.h” library.
– The list of these functions can be found in the next slide.
11. Lectures on Busy Bee Workshop 11
String in C – Library FunctionsString in C – Library Functions
Function Purpose Example
strcpy Makes a copy of a string strcpy(s1, “Hi”);
strcat Appends a string to the
end of another string
strcat(s1, “more”);
strcmp Compare two strings
alphabetically
strcmp(s1, “Hu”);
strlen Returns the number of
characters in a string
strlen(“Hi”) returns
2.
strtok Breaks a string into
tokens by delimiters.
strtok(“Hi, Chao”, “ ,”);
12. Lectures on Busy Bee Workshop 12
String in C – Library FunctionsString in C – Library Functions
Function Purpose Example
Strncpy Copy the specified
number of characters
strncpy(s1,
“SVN”,2);
Strncmp Compare two string upto
given n character
strncmp(“mo”,
“more”,2);
Stricmp Compare two strings
alphabetically without case
sensitivity.
stricmp(“hu”, “Hu”);
strlwr Converts string to all
lowercase
strlwr(“Hi”) returns
hi.
strupr Converts s to all
uppercase
strupr(“Hi”);
13. Lectures on Busy Bee Workshop 13
String in C – Library FunctionsString in C – Library Functions
Function Purpose Example
Strncat Appends a string to the
end of another string up
to n characters
strncat(s1,
“more”,2);
Strrev Reverses all characters
in s1 (except for the
terminating null)
strrev(s1, “more”);
14. Lectures on Busy Bee Workshop 14
String in C – Example program (Library Function)String in C – Example program (Library Function)