Difference between revisions of "C-and-C++/C3/String-Library-Functions/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(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 …')
 
Line 1: Line 1:
'''Title of script''': Strings
+
'''Title of script''': String Library Functions
  
'''Author: '''Ashwini R Patil
+
'''Author:''' Ashwini R. Patil
  
'''Keywords: Strings in C, getline, Video Tutorial'''
+
'''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 '''Strings''' in '''C '''and''' C++'''
 
  
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 2
 
  
  
 +
| 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
  
| 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,
+
Library Functions in C
  
What is a string.  
+
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 2
  
Declaration of the string.
+
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,
  
Initialization of the string.
+
String Library Functions
  
With the help of an example.
+
We will do this with the help of some examples
 
+
We will aldo see some common errors and their solutions
+
  
 
|-
 
|-
 
| 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.04
+
Ubuntu Operating System version 11.10,
  
'''gcc''' and '''g++''' '''Compiler''' version 4.6.1 on ubuntu
+
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
| 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 start with the Introduction to the '''Strings'''
 
  
'''String''' is a sequence of characters that is treated as a single data item
+
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.
  
size of '''string''' = original length of '''string''' + 1
+
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;"| Slide 5
+
 
+
Declaration of the String
+
 
+
Syntax:
+
 
+
<nowiki>char val[10];</nowiki>
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let me tell you how to declare a '''string'''
+
 
+
The syntax for this is
+
 
+
'''<nowiki>char name_of_string[size]; </nowiki>'''
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Please open the Terminal
+
| 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="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 declare a '''string''' with an
 
  
example
+
| 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
  
I have already typed the program
+
Here we have the strncpy function
  
So I will just open it
+
The syntax for this is strncpy(char str1, char str2, int n )
  
Note that I have saved the file with the name '''string.c'''
+
It copies first n characters of string str2 into string str1.
  
Let me explain the code now
+
eg.
  
|-
+
char strncpy(char hello, char world, 2);
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
+
  
'''<nowiki>#include<string.h></nowiki>'''
+
The output will be
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here, '''string.h''' is a header file
+
  
It includes the declarations, functions, constants of '''string''' handling utilities
+
Wollo
  
Whenever we work on '''string '''functions, we should include this header file
+
Here we have Wo fromt 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;"| Highlight
+
| 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
  
'''<nowiki>char strname[30]</nowiki>'''<nowiki>; </nowiki>
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we are declaring the '''string''' ''''strname'''' with size ''''30''''
 
  
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
 
 
'''printf("Enter the string\n"); '''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we are accepting a '''string''' from the user
 
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
 
  
'''<nowiki>scanf("%[^\n]s",strname);</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;"| 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.
  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To read a '''string''', we can use '''scanf()''' function with format specifier '''%s'''
+
int strncmp(char ice, char icecream, int 2);
  
Here we are using the '''caret''' sign and''' \n''' to include the spaces with the '''string'''
+
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;"| Type:
+
| 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
  
'''printf("The string is %s\n", strname);'''
+
Please open the Text Editor
| style="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 '''string'''
+
  
|-
+
Go to Applications – Accessories – Text Editor
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Save the program
+
| 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.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now click on '''Save''' to save the program
+
  
|-
+
I am going to show you some of the commonly used string functions.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Execution of the program
+
  
On the terminal
+
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:1pt solid #000000;padding:0.097cm;"| Let us execute the program
+
 
+
Please open the terminal window by pressing '''Ctrl, Alt and T '''keys simultaneously
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type
+
| 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.
  
'''gcc strdeclare.c str'''
+
In this we will find the length of the string
  
>> press '''Enter'''
+
These are the header files as '''stdio.h and string.h '''Make only one file which contain all the code / function.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To compile
+
  
Type
+
This is our main function
 
+
'''gcc strdeclare.c str'''
+
 
+
And press '''Enter'''
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
  
'''./str '''
+
'''<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'''',
  
>> press '''Enter'''
+
It stores a value ''''Ashwini''''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To execute,
+
  
type
+
Then we have an integer variable '''len1 '''
  
'''./str '''
 
  
and press '''Enter'''
 
  
|-
 
| style="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;"| '''Enter the string '''is displayed here
 
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
 
+
'''Talk To A Teacher >>''' press '''Enter'''
+
 
+
 
+
 
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| I will type
+
  
'''Talk To A Teacher'''
+
int len1 = strlen(arr);
 +
| 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 lenght of the string using strlen funtion
  
and press '''Enter'''
+
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
  
Output
+
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;"| And the output is displayed as
+
| 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.
  
'''The string is Talk To A Teacher'''
+
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;"| Slide 6
 
  
  
Initialization of the string
 
 
 
Syntax:
 
 
 
<nowiki>char strinitialize[20] = “star”;</nowiki>
 
 
 
(or)
 
 
 
<nowiki>char strinitialize[] = {'S', 't', 'r'};</nowiki>
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us switch back to the slides
 
 
 
Until now we discussed about the declaration of a '''string'''
 
 
Now we are going to discuss how to initialize the '''string'''
 
 
This is done by specifying the value
 
 
To initialize a string the syntax is
 
 
'''<nowiki>char var_name[size] = “string”;</nowiki>'''
 
 
(or)
 
 
'''<nowiki>char var_name[] = </nowiki>{'S', 't', 'r'};'''
 
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Please switch to the Text editor
+
| 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
  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let me show you how to use the first syntax with an example
+
Open the terminal window by pressing
  
Please switch back to the Text Editor
+
'''Ctrl, Alt''' and '''T''' keys simultaneously on your keyboard
 
+
Let me use the same example
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Click on
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type
  
File >> Save as option
+
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  
+
Type:
  
strinitialize.c
+
"gcc" space "strlen.c" space “-o” space “str1”.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| First click on '''File''' menu
+
  
Click on '''Save as''' option
+
Press Enter
 
+
Let us save the file with the name '''strinitialize.c'''
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Click on save
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now click on '''Save'''
+
 
+
|-
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type
+
 
+
 
+
'''<nowiki>char name[30] = "Talk To A Teacher"</nowiki>'''<nowiki>; </nowiki>
+
 
+
 
+
 
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us change a few things here
+
  
Let us replace the 5th line with
+
'''./str1 '''
 +
| 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:
  
'''char <nowiki>strname [30] = “Talk To A Teacher”;</nowiki>'''
+
'''./str1'''  
  
Here we have initialized a character array '''strname''' with size ''''30''''
+
Press Enter
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Delete
+
| 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("Enter the string\n"); '''
+
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
  
'''<nowiki>scanf("%[^\n]s",strname);</nowiki>'''
+
string = Ashwini, Length = 7
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Please remove the next two lines
+
  
as we only want to print the '''string'''
+
You can count here. 1,2,3,4,5,6, and 7
 
+
We are not taking the '''string''' as input from the user
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Save the program
+
| 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;"| Now let us save the program and execute it
+
| 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;"| On the terminal
+
 
+
Type
+
 
+
'''gcc strinitialize.c -o str1'''
+
 
+
 
+
 
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Come back to the terminal
+
 
+
Let us compile as before
+
 
+
Type
+
 
+
'''gcc strinitialize.c -o str1'''
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| To execute
+
| 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="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
Type
+
 
+
'''./str1'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To execute
+
 
+
Type
+
 
+
'''./str1'''
+
  
 
|-
 
|-
| 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
  
'''Output'''
+
strcpy(target, source);
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here the output is displayed as
+
| 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
  
'''The string is Talk To A Teacher'''
+
Here we have Ice in the source string, it will be copied to the target string  
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Error1
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
  
Type
+
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  
'''sting.h'''
+
| style="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 common errors which we can come across
+
 
+
Suppose here we type the spelling of string as
+
 
+
'''sting'''
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Click on '''Save'''
+
| 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
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| I will retain rest of the code as it is
+
  
Click on '''Save'''
+
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 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;"| On the terminal
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To execute the program, we will go on the terminal
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Compile and execute as before
+
| 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'''
  
'''gcc strinitialize.c -o str1'''
+
Press '''Enter'''
  
 +
Type
  
'''./str1'''
+
'''./str2 '''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us compile and execute as before
+
  
We see that there is a fatal error
+
Press''' Enter'''
  
The compiler is not able to find the '''header file''' with the name '''<nowiki><sting.h></nowiki>'''
+
Everywhere you change the file name and tell them to compile as before. Yes, i want an explanation?
  
Hence the compilation is terminated
+
<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;"| On the editor, correct the spelling
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us fix this error
+
  
Come back to the program
+
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
  
Type '''r''' here
+
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;"| On the terminal
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Come back on the terminal
+
 
+
Let us compile and execute as before
+
 
+
Yes it is working!
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Error2
+
| 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;"| Now let us see another string function
Type
+
 
+
'''int'''
+
| style="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 common error
+
 
+
Suppose I type '''int''' here in place of''' char'''
+
 
+
Let us see what happens
+
 
+
Come back on the terminal
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal
+
| 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="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us compile and execute as before
+
| 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
  
We see there is an error and a warning
+
Note that our filename is strcmp.c
  
'''The format specifier for string is %s '''
+
In this we will comapre two strings
  
And we are declaring it with an integer data type '''int'''
+
Here we have character variables as str1 and str2
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the editor
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us fix the error
+
  
Come back to our program
+
<nowiki>char source[]= "World"; </nowiki>
  
Let us type '''char '''here
+
<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;"| str 1 stores the value as ''''Ice'''' and str2 stores the value as ''''Cream'.'''
  
|-
+
Here we have interger variables as i and j
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Click on save
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Click on '''Save'''
+
  
|-
+
In this we will compare the string using the strcmp function
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal
+
| style="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 terminal
+
  
Let us compile and execute as before
+
Here we compare str1 ie: 'Ice' with 'Hello'
  
|-
+
The result is stored in i
| style="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;"| Yes it is working!
+
  
|-
+
In this we will compare string 2 ie: 'Cream' with 'Cream'
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the editor
+
| style="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 HOW TO EXECUTE THE SAME PROGRAM IN C++
+
  
Come back to our program
+
The result is stored in j
  
|-
+
Then we print both the results
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Click on File
+
  
 +
and this is our return statement
  
Save as
 
  
Type
 
  
'''.cpp'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| First let us save the file with an extention '''.cpp'''
 
 
Click on '''File''' menu
 
 
Click on '''Save as '''option
 
 
Now save the file with an extension '''.cpp'''
 
  
 
|-
 
|-
| 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
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let me change a few things here
+
  
|-
+
Please switch back to the terminal
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type
+
| 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.
  
iostream
+
Come back to our terminal.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| First we will change the header file as
+
 
+
'''iostream'''
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type
+
| 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 '''strcmp.c '''space '''-o''' space '''str3 '''
  
'''<nowiki>#include <string></nowiki>'''
+
Press '''Enter'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We will include another header file here
+
  
 
Type  
 
Type  
  
'''<nowiki>#include <string></nowiki>'''
+
'''./str3'''
 
+
|-
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type
+
  
using namespace std;
+
The outpur is displayed as 1,0  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We will include the '''using '''statement here
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type
+
| 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
  
string strname;
+
Here we get 1
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us define a string variable '''strname'''
+
 
+
Since we don't use the '''format specifier''' in C++ ,
+
 
+
the compiler should know that '''strname''' is a '''string''' variable
+
 
+
|-
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type
+
 
+
<nowiki>cout <<</nowiki>
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now replace the '''printf '''statement with '''cout''' statement
+
  
 +
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;"| Delete scanf
+
| 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,
  
Type
+
String library functions
  
'''getline(cin, strname);'''
+
strlen()
  
 +
strcpy()
  
 +
strcmp()
  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now delete the '''scanf''' statement here and
+
strncpy()
  
Type
+
and strncmp()
  
'''getline(cin, strname);'''
 
  
|-
 
| style="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;"| We use '''getline''' to extract the characters from the input sequence
 
  
It stores them as a '''string'''
 
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type
+
| 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
  
<nowiki>cout <<"The string is " <<strname <<"\n";</nowiki>
+
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.
  
In place of
+
Hint: strcat(char str1, char str2);
  
printf("The string is %s\n",strname);
+
Also explore the other functions in string library.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now again replace the '''printf''' statement with '''cout''' statement
+
 
+
Delete the format specifier and '''\n''' here
+
 
+
Type two opening angle brackets
+
 
+
Then again type two opening angle brackets here and type within the double quotes '''\n'''
+
 
+
Delete the closing bracket here
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Click on save
+
| 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
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now click on '''Save'''
+
  
|-
+
About the Spoken Tutorial Project
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Come back on the terminal
+
  
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type
 
  
'''g++ string.cpp -o str1'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To compile type
 
  
'''g++ string.cpp -o str1'''
+
| 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="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type
+
 
+
'''./str1'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To execute type
+
 
+
'''./str1'''
+
 
+
|-
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight >> type '''Talk To A Teacher'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Enter the string '''is displayed here
+
 
+
I will type''' Talk To A Teacher'''
+
 
+
|-
+
| style="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;"| The output is displayed as
+
 
+
'''The string is Talk To A Teacher'''
+
 
+
|-
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 7
+
 
+
Assignment
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| As an assignment
+
 
+
Write a program to print a string using the 2<sup>nd</sup> syntax
+
 
+
|-
+
| style="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="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  
+
  
 
http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial
 
http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial
Line 587: Line 371:
 
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 380:
  
  
| 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 386:
 
Gives certificates to those who pass an online test  
 
Gives certificates to those who pass an online test  
  
For more details, Please write to
+
For more details, please write to  
  
contact@spoken-tutorial.org
+
<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
Line 618: Line 402:
  
 
|-
 
|-
| 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 You for joining
+
Thank you for joining.
  
 
|}
 
|}

Revision as of 13:50, 3 May 2013

Title of script: String Library Functions

Author: Ashwini R. Patil

Keywords: String Library Functions, strcpy, strcat, strlen, Video Tutorial


Visual Cue
Narration


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


Ctrl, Alt and T keys simultaneously

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


Acknowledgement

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.

Contributors and Content Editors

Ashwini