C-and-C++/C3/String-Library-Functions/English
Title of script: String Library Functions
Author: Ashwini R. Patil
Keywords: String Library Functions, strcpy, strcat, strlen, Video Tutorial
|
|
Slide 1
|
Welcome to the spoken-tutorial on String
Library Functions in C |
Slide 2
Learning Objectives |
In this tutorial, we will learn,
String Library Functions We will do this with the help of some examples |
Slide 3
System Requirements |
To record this tutorial, I am using
Ubuntu Operating System version 11.10, gcc Compiler Version 4.6.1 |
Slide 4
Introduction to the standard library funtions |
Let us start with an introduction to string library functions.
These are the group of functions implementing operations on strings. Various operations such as copying, concatenation, searching etc are supported. |
Slide 5
String Library functions
|
Let us see some of the string library functions
Here we have the strncpy function The syntax for this is strncpy(char str1, char str2, int n ) It copies first n characters of string str2 into string str1. eg. char strncpy(char hello, char world, 2); The output will be Wollo Here we have Wo fromt the string 2 and rest of the characters from string 1 |
Slide 6
|
Now we will see strcmpy function int strncmp, the syntax for this is (char str1, char str2, int n)
It will compare first n characters of string 2 with string 1 eg. int strncmp(char ice, char icecream, int 2); The output will be 0 |
Examples of some commonly used String functions
Please open the Text Editor Go to Applications – Accessories – Text Editor |
Now we will see how to use the string library functions.
I am going to show you some of the commonly used string functions. I have already typed the program on the editor, i will open it Here we have the string length function |
//strlen() | Note that our filename is strlen.c.
In this we will find the length of the string These are the header files as stdio.h and string.h Make only one file which contain all the code / function. This is our main function |
Highlight
char arr[] = "Icecream"; |
Here we have a character variable 'arr',
It stores a value 'Ashwini' Then we have an integer variable len1
|
Highlight
int len1 = strlen(arr); |
here we will find the lenght of the string using strlen funtion
the result will be stored in len1 |
Highlight
printf("string = %s length = %d\n", arr, len1); |
Then we print the string and the length of the string.
And this is our return statement
|
Execution of the program
|
Now let us execute the program
Open the terminal window by pressing Ctrl, Alt and T keys simultaneously on your keyboard |
Type
gcc strlen.c -o len |
To compile
Type: "gcc" space "strlen.c" space “-o” space “str1”. Press Enter |
Type
./str1 |
type:
./str1 Press Enter |
Highlight
Output |
The output is displayed as
string = Ashwini, Length = 7 You can count here. 1,2,3,4,5,6, and 7 |
Let us see another string function
Here we have the string copy fuction | |
//strcpy | Note that our filename is strcpy.c |
Highlight
strcpy(target, source); |
In this we will copy the source string into the target string
Here we have Ice in the source string, it will be copied to the target string |
Highlight
printf("source string = %s\n",source); |
Here we print the source string and the target string |
Execution of the program
Please Switch back to the terminal |
Let us execute and see
Come back to our terminal |
To compile type
gcc space strcpy.c space -o space str2 Press Enter Type ./str2 Press Enter Everywhere you change the file name and tell them to compile as before. Yes, i want an explanation? * Ignore this, i deleted. | |
Highlight
Output |
The output is displayed as
source string = Ice target string = Ice |
Now let us see another string function | |
//strcat | Now we will see the string compare function
Note that our filename is strcmp.c In this we will comapre two strings Here we have character variables as str1 and str2 |
Highlight
char source[]= "World"; char target[]= "Hello"; |
str 1 stores the value as 'Ice' and str2 stores the value as 'Cream'.
Here we have interger variables as i and j In this we will compare the string using the strcmp function Here we compare str1 ie: 'Ice' with 'Hello' The result is stored in i In this we will compare string 2 ie: 'Cream' with 'Cream' The result is stored in j Then we print both the results and this is our return statement
|
Execution of the program
Please switch back to the terminal |
Let us execute the program.
Come back to our terminal. |
To compile type
gcc space strcmp.c space -o space str3 Press Enter Type ./str3 The outpur is displayed as 1,0 | |
Come back to our program
Here we get 1 and here we get as 0 Let us come back to our slides Let us summarize, | |
Summary | In this tutorial we learned,
String library functions strlen() strcpy() strcmp() strncpy() and strncmp()
|
Slide 13
Assignment |
As an assignemnt,
Write a C Program to concatenate String best and String bus. Hint: strcat(char str1, char str2); Also explore the other functions in string library. |
Slide 8
About the Spoken Tutorial Project
|
Watch the video available at the link shown
http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial It summarises the Spoken Tutorial project If you do not have good bandwidth, you can download and watch it. |
Slide Number 9
Spoken Tutorial Workshops
|
The Spoken Tutorial Project Team
Conducts workshops using spoken tutorials Gives certificates to those who pass an online test For more details, please write to contact [at] spoken hyphen tutorial dot org |
Slide Number 10
|
Spoken Tutorial Project is a part of the Talk to a Teacher project
It is supported by the National Mission on Education through ICT, MHRD, Government of India More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro |
This is Ashwini Patil from IIT Bombay.
Thank you for joining. |