Difference between revisions of "C-and-C++/C3/String-Library-Functions/English"
(Created page with ''''Title of script''': Strings '''Author: '''Ashwini R Patil '''Keywords: Strings in C, getline, Video Tutorial''' {| style="border-spacing:0;" | style="border-top:0.002cm …') |
|||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
− | '''Title of script''': | + | '''Title of script''': String Library Functions |
− | '''Author: '''Ashwini R Patil | + | '''Author:''' Ashwini R. Patil |
− | '''Keywords: | + | '''Keywords:''' '''String Library Functions, strcpy, strcat, strlen, Video Tutorial''' |
Line 10: | Line 10: | ||
| style="border-top:0.002cm solid #000000;border-bottom:0.002cm solid #000000;border-left:0.002cm solid #000000;border-right:none;padding:0.097cm;"| <center>'''Visual Cue'''</center> | | style="border-top:0.002cm solid #000000;border-bottom:0.002cm solid #000000;border-left:0.002cm solid #000000;border-right:none;padding:0.097cm;"| <center>'''Visual Cue'''</center> | ||
| style="border:0.002cm solid #000000;padding:0.097cm;"| <center>'''Narration'''</center> | | style="border:0.002cm solid #000000;padding:0.097cm;"| <center>'''Narration'''</center> | ||
+ | |||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 1 | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 1 | ||
− | |||
− | |||
− | |||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Welcome to the spoken-tutorial on '''String Library Functions in C''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right: | + | |- |
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 2 | ||
− | + | Learning Objectives | |
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial, we will learn, | ||
− | + | String Library Functions | |
− | + | We will do this with the help of some examples | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 3 | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 3 | ||
− | + | System Requirements | |
− | + | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To record this tutorial, I am using | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To record this tutorial, I am using | ||
− | + | Ubuntu Operating System version 11.10, | |
− | + | gcc Compiler Version 4.6.1 | |
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 4 | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 4 | ||
− | |||
− | + | Introduction to the standard library funtions | |
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| 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. | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 5 |
+ | String Library functions | ||
− | |||
− | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| 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, (and) 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''' from the string 2 and rest of the characters from string 1 | |
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 6 |
− | |||
− | |||
− | |||
− | |||
− | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now we will see '''strcmpy''' function , the syntax for this is | |
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | |
− | + | '''(char str1, char str2, int n)''' | |
− | + | ||
− | + | It will compare first n characters of string 2 with string 1 | |
+ | eg. | ||
+ | '''int strmcmp(char ice, char icecream, (and) int 2);''' | ||
− | + | The output will be 0 | |
− | + | ||
− | + | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Examples of some commonly used String functions |
− | + | Please open the Text Editor | |
− | + | ||
− | + | Go to Applications – Accessories – Text Editor | |
− | | style=" | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| 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 | |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| //strlen() |
+ | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| 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 ''' | |
− | + | ||
− | + | This is our main function | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight |
− | ''' | + | '''<nowiki>char arr[] = "Icecream";</nowiki>''' |
+ | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we have a character variable ''''arr'''', | ||
− | + | It stores a value ''''Ashwini'''' | |
− | + | ||
− | + | Then we have an integer variable '''len1 ''' | |
− | |||
− | |||
− | |||
|- | |- | ||
− | | style=" | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight |
− | + | ||
− | + | int len1 = strlen(arr); | |
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right: | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here''' '''we will find the length of the string using strlen funtion |
− | + | the result will be stored in len1 | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight |
− | + | printf("string = %s length = %d\n", arr, len1); | |
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we print the string and the length of the string. |
− | + | And this is our return statement | |
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Execution of the program |
− | + | '''Ctrl, Alt''' and '''T''' keys simultaneously | |
+ | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let us execute the program | ||
+ | Open the terminal window by pressing | ||
− | + | '''Ctrl, Alt''' and '''T''' keys simultaneously on your keyboard | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type |
+ | gcc strlen.c -o len | ||
+ | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To compile | ||
+ | Type: | ||
− | + | "gcc" space "strlen.c" space “-o” space “str1”. | |
− | + | '''Press Enter''' | |
− | + | ||
− | + | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type |
− | + | ||
− | + | ||
− | + | ||
− | Type | + | |
− | + | '''./str1 ''' | |
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| type: |
− | + | '''./str1''' | |
− | + | '''Press Enter ''' | |
|- | |- | ||
− | | style=" | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight |
− | + | ||
− | + | Output | |
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right: | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The output is displayed as |
+ | string = Ashwini, Length = 7 | ||
− | + | You can count here. 1,2,3,4,5,6, and 7 | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| |
+ | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us see another string function | ||
− | + | Here we have the string copy fuction | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| //strcpy |
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Note that our filename is '''strcpy.c''' |
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight |
− | + | strcpy(target, source); | |
+ | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| 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 | |
− | + | This is our '''strcpy''' function. | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | ''' | + | |
|- | |- | ||
− | | style=" | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | printf("source string = %s\n",source); | |
+ | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we print the source string and the target string | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Execution of the program |
− | + | Please Switch back to the terminal | |
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us execute and see |
− | + | Come back to our terminal | |
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| |
+ | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| 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? | |
− | + | <nowiki>* Ignore this, i deleted.</nowiki> | |
− | + | ||
− | + | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight |
+ | Output | ||
+ | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The output is displayed as | ||
− | + | source string = Ice | |
− | + | target string = Ice | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| |
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let us see another string function |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
− | | style=" | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| //strcat |
− | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now we will see the string compare function | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | | style=" | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | Note that our filename is '''strcmp.c''' | |
− | + | In this we will comapre two strings | |
− | + | Here we have character variables as '''str1''' and '''str2 ''' | |
|- | |- | ||
− | | style=" | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight |
− | + | ||
− | + | <nowiki>char source[]= "World"; </nowiki> | |
− | ''' | + | <nowiki>char target[]= "Hello";</nowiki> |
+ | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''str1''' 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. | |
− | + | ||
− | + | ||
− | |||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Execution of the program |
+ | Please switch back to the terminal | ||
+ | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us execute the program. | ||
− | + | Come back to our terminal. | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| |
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To compile type |
− | + | '''gcc''' space '''strcmp.c '''space '''-o''' space '''str3 ''' | |
− | + | ||
− | + | Press '''Enter''' | |
− | + | ||
− | + | ||
− | ''' | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
Type | Type | ||
− | ''' | + | '''./str3''' |
− | + | ||
− | + | ||
− | + | ||
− | + | The outpur is displayed as 1,0 | |
− | + | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| |
+ | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| 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, | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Summary |
+ | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial we learned, | ||
− | + | String library functions | |
− | + | strlen() | |
+ | strcpy() | ||
+ | strcmp() | ||
− | + | strncpy() | |
− | + | and strncmp() | |
− | |||
|- | |- | ||
− | | style=" | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 13 |
− | + | ||
− | + | Assignment | |
+ | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| 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. | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
− | | style=" | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 8 |
− | + | ||
− | + | About the Spoken Tutorial Project | |
− | + | ||
− | + | ||
− | |||
− | |||
− | |||
− | |||
− | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Watch the video available at the link shown | |
− | + | ||
− | + | ||
− | | style=" | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial | http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial | ||
Line 587: | Line 364: | ||
It summarises the Spoken Tutorial project | It summarises the Spoken Tutorial project | ||
− | If you do not have good bandwidth, you can download and watch it | + | If you do not have good bandwidth, you can download and watch it. |
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 9 | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide Number 9 |
Spoken Tutorial Workshops | Spoken Tutorial Workshops | ||
Line 596: | Line 373: | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The Spoken Tutorial Project Team | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The Spoken Tutorial Project Team |
Conducts workshops using spoken tutorials | Conducts workshops using spoken tutorials | ||
Line 602: | Line 379: | ||
Gives certificates to those who pass an online test | Gives certificates to those who pass an online test | ||
− | For more details, | + | For more details, please write to |
− | contact | + | <nowiki>contact [at] spoken hyphen tutorial dot org</nowiki> |
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 10 | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide Number 10 |
Acknowledgement | Acknowledgement | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Spoken Tutorial Project is a part of the Talk to a Teacher project | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| 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 | 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 | + | More information on this Mission is available at the link shown below: http://spoken-tutorial.org\NMEICT-Intro |
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| |
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Ashwini Patil from IIT Bombay | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is Ashwini Patil from IIT Bombay. |
− | Thank | + | Thank you for joining. |
|} | |} |
Latest revision as of 12:57, 31 January 2014
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, (and) 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 from the string 2 and rest of the characters from string 1 |
Slide 6
|
Now we will see strcmpy function , 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 strmcmp(char ice, char icecream, (and) 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 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 length 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 This is our strcpy function. |
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"; |
str1 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 the link shown below: http://spoken-tutorial.org\NMEICT-Intro |
This is Ashwini Patil from IIT Bombay.
Thank you for joining. |