Difference between revisions of "C-and-C++/C3/Strings/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 4: Line 4:
  
 
'''Keywords: Strings in C, getline, Video Tutorial'''
 
'''Keywords: Strings in C, getline, Video Tutorial'''
 +
  
  
 
{| style="border-spacing:0;"
 
{| style="border-spacing:0;"
| 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>
+
! <center>Visual Cue</center>
| style="border:0.002cm solid #000000;padding:0.097cm;"| <center>'''Narration'''</center>
+
! <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: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: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,  
 
| 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,  
  
*What is a '''string'''.  
+
What is a string.  
*Declaration of the '''string'''.
+
 
*Initialization of the '''string'''.  
+
Declaration of a string.
*With the help of an example.
+
 
 +
Initialization of a string.  
 +
 
 +
Few examples on string.
  
We will aldo see some common errors and their solutions  
+
We will also 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.04  
*'''gcc''' and '''g++''' '''Compiler''' version 4.6.1
+
 
 +
'''gcc''' and '''g++''' '''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'''
+
 
 +
Introduction to strings
 +
| 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
 
'''String''' is a sequence of characters that is treated as a single data item
  
Size of '''string''' = length of '''string''' + 1
+
size of '''string''' = length of '''string''' + 1
  
 
|-
 
|-
Line 45: Line 59:
 
Declaration of the String
 
Declaration of the String
  
Syntax: <nowiki>char val[10];</nowiki>
+
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'''
 
| 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'''
  
Line 51: Line 67:
  
 
'''<nowiki>char name_of_string[size]; </nowiki>'''
 
'''<nowiki>char name_of_string[size]; </nowiki>'''
*'''char''' is the data-type
 
*'''name_of_string''' is the '''string''' name
 
* and we can give the size here
 
  
Eg- Here we have declared a '''character string "names"''' with size 10.  Now we will see an example.
+
char, name of string and size
  
 +
char is the data type, name of the string is the string name, and we can give the size here.
 +
 +
'''Eg: here we have declared a character string names with size 30 '''
  
 
|-
 
|-
Line 63: Line 79:
  
  
| 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 an  
+
| 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 an example
  
example
+
I have already typed the program
  
I have already typed the program.
+
I will open it
  
So I will open it.
+
Note that our file name is '''string.c'''
  
Note that our filename is '''string.c'''
+
'''In this program we will take a string as an input from the user and print it'''
 
+
|-
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|
+
 
+
'''<nowiki>#include<string.h></nowiki>'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|In this program, we will take a '''string''' as input from the user and print it.
+
  
Let me explain the code now.
+
Let me explain the code now
  
 
|-
 
|-
Line 85: Line 95:
  
 
'''<nowiki>#include<string.h></nowiki>'''  
 
'''<nowiki>#include<string.h></nowiki>'''  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| These are our header files.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| These are out header files  
  
Here, '''string.h''' includes the declarations, functions, constants of '''string''' handling utilities.
+
here '''string.h''' includes the declarations, functions, constants of '''string''' handling utilities
  
Whenever we work on '''string functions''', we should include this header file.
+
Whenever we work on '''string '''functions, we should include this header file
  
 
|-
 
|-
Line 95: Line 105:
  
 
'''<nowiki>char strname[30]</nowiki>'''<nowiki>; </nowiki>
 
'''<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;"| This is our '''main function'''.  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:1pt solid #000000;padding:0.097cm;"| This is our main function  
 +
 
 +
Here we are declaring the '''string''' ''''strname'''' with size ''''30''''
  
 
|-
 
|-
Line 101: Line 113:
  
 
'''printf("Enter the string\n"); '''
 
'''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:1pt solid #000000;padding:0.097cm;"| Here we are accepting a '''string''' from the user
  
 
|-
 
|-
Line 112: Line 124:
 
| 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'''
 
| 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'''
  
Here we are using the '''caret''' sign and''' \n''' to include the spaces with the '''string'''.
+
We are using the '''caret''' sign and''' \n''' to include the spaces with the '''string'''
  
 
|-
 
|-
Line 118: Line 130:
  
 
'''printf("The string is %s\n", strname);'''  
 
'''printf("The string is %s\n", strname);'''  
| style="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'''.
+
| style="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 this is our return statement
  
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;"| Save the program
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Save the program
| 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:1pt solid #000000;padding:0.097cm;"| Now click on '''Save'''  
  
 
|-
 
|-
Line 134: Line 147:
 
| 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
 
| 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 on your keyboard.
+
Please open the terminal window by pressing '''Ctrl, Alt and T '''keys simultaneously on your keyboard
  
 
|-
 
|-
Line 142: Line 155:
  
 
>> press '''Enter'''
 
>> press '''Enter'''
| 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
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To compile
  
'''gcc strdeclare.c str'''
+
Type
  
And press '''Enter'''.
+
'''gcc '''space''' string.c '''space''' -o '''space '''str'''
 +
 
 +
And press '''Enter'''
  
 
|-
 
|-
Line 154: Line 169:
  
 
>> press '''Enter'''
 
>> press '''Enter'''
| 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  
+
| 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  
  
 
'''./str '''
 
'''./str '''
  
and press '''Enter'''.
+
now 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: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;"| Here it is displayed as '''Enter the string '''.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here it is displayed as '''Enter the string '''
  
 
|-
 
|-
Line 175: Line 192:
 
'''Talk To A Teacher'''
 
'''Talk To A Teacher'''
  
Now, press '''Enter'''.
+
now press '''Enter'''
  
 
|-
 
|-
Line 181: Line 198:
  
 
Output
 
Output
| 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="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'''
 
'''The string is Talk To A Teacher'''
Line 187: Line 204:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 6
 
| 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
 
Initialization of the string
 +
  
 
Syntax:
 
Syntax:
 +
  
 
<nowiki>char strinitialize[20] = “star”;</nowiki>
 
<nowiki>char strinitialize[20] = “star”;</nowiki>
 +
  
 
(or)
 
(or)
 +
  
 
<nowiki>char strinitialize[] = {'S', 't', 'r'};</nowiki>
 
<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.
+
| 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 switch back to our slides
  
  
Until now, we discussed about the declaration of a '''string'''.
+
Until now we discussed about the declaration of a '''string'''
  
Now we are going to discuss how to initialize the '''string'''.
+
Now we are going to discuss how to initialize the '''string'''
  
 
The syntax for this is
 
The syntax for this is
Line 208: Line 230:
 
'''<nowiki>char var_name[size] = “string”;</nowiki>'''
 
'''<nowiki>char var_name[size] = “string”;</nowiki>'''
  
Eg- Here we have declared a '''character string "names"''' with size 10 and the string is '''"Priya"'''.
+
eg: here we have declared a character string names with size 30 and the string is Priya
 +
 
 +
Another syntax is
 +
 
 +
'''<nowiki>char var_name[ ] = </nowiki>{'S', 't', 'r', 'i', 'n', 'g'}; '''within single quotes
 +
 
 +
'''<nowiki>eg: char names[10] = {'P', 'r', 'i', 'y', 'a'}; </nowiki>'''in single quotes
  
  
Another syntax is
 
'''<nowiki>char var_name[] = </nowiki>{'S', 't', 'r', 'i', 'n', 'g'};'''
 
  
Eg-char names 10 is equal to Priya within single quotes.
 
  
 
|-
 
|-
Line 221: Line 246:
  
  
| 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.
+
| 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
  
Switch back to the Editor.
+
Switch back to our Editor  
 +
 
 +
We will use the same example
  
We will use the same example.
 
  
|-
 
| 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;"|
 
  
<span style= "color:red">I CANNOT EDIT THIS SCRIPT ANYMORE.  ASHWINI, PLEASE UPDATE THE SCRIPT TO MATCH THE NARRATION OF THE VIDEO ON THE UPLOAD INTERFACE.</span>
 
  
 
|-
 
|-
Line 241: Line 263:
  
 
strinitialize.c
 
strinitialize.c
| 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
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| First press shift, cntrl and s key simultaneously on your keyboard
  
Click on '''Save as''' option
+
now save the file with the name '''strinitialize.c '''
 
+
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="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;"| Now 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;"| Now click '''Save'''
  
 
|-
 
|-
Line 259: Line 279:
  
  
| 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
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We are going to intialize this string
 +
 
 +
Hence at the 5th line type
 +
 
 +
'''<nowiki>= and within </nowiki>double quotes “Spoken- Tutorial”;'''
 +
 
 +
now click on save
  
Let us replace the 5th line with
 
  
'''char <nowiki>strname [30] = “Talk To A Teacher”;</nowiki>'''
 
  
Here we have initialized a character array '''strname''' with size ''''30''''
 
  
 
|-
 
|-
Line 273: Line 296:
  
 
'''<nowiki>scanf("%[^\n]s",strname);</nowiki>'''
 
'''<nowiki>scanf("%[^\n]s",strname);</nowiki>'''
| 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
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now remove these two lines
  
as we only want to print the '''string'''
+
as we are only going to print the '''string'''
  
We are not taking the '''string''' as input from the user
+
'''click on save'''
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Save the program
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Save the program
| 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="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| let us execute  
  
 
|-
 
|-
Line 292: Line 315:
  
  
| 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  
+
| 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 as before
+
To compile  
  
 
Type  
 
Type  
  
'''gcc strinitialize.c -o str1'''
+
'''gcc space stringintialize.c '''space '''-o '''space''' str2'''
  
 
|-
 
|-
Line 306: Line 329:
  
 
'''./str1'''
 
'''./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
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we have str2 because we dont want to overwrite the output parameter '''str''' for the file '''string.c'''
  
Type
+
now press '''Enter '''
  
'''./str1'''
+
'''To execute type ./str2 '''
  
 
|-
 
|-
Line 316: Line 339:
  
 
'''Output'''
 
'''Output'''
| 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="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'''
+
'''The string is Spoken-Tutorial'''
  
 
|-
 
|-
Line 326: Line 349:
  
 
'''sting.h'''
 
'''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
+
| 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 some common errors which we can come across
 +
 
 +
Come back to our program
  
 
Suppose here we type the spelling of string as
 
Suppose here we type the spelling of string as
Line 334: Line 359:
 
|-
 
|-
 
| 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: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;"| I will retain rest of the code as it is
+
| 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'''
 
+
Click on '''Save'''
+
  
 
|-
 
|-
 
| 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: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:1pt solid #000000;padding:0.097cm;"| Let us execute, come back to the terminal
  
 
|-
 
|-
Line 350: Line 373:
  
 
'''./str1'''
 
'''./str1'''
| 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="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now compile as before  
  
We see that there is a fatal error  
+
We see a fatal error  
  
The compiler is not able to find the '''header file''' with the name '''<nowiki><sting.h></nowiki>'''
+
'''sting.h no such file or directory'''
  
Hence the compilation is terminated  
+
compilation terminated  
  
 
|-
 
|-
 
| 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="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="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
+
| 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 program
  
Come back to the program
+
This is because the compiler is not able to find the header file with the name sting.h
 +
 
 +
Hence it is giving an error
 +
 
 +
Let us fix the error
  
 
Type '''r''' here
 
Type '''r''' here
 +
 +
now click on save
  
 
|-
 
|-
 
| 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: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:1pt solid #000000;padding:0.097cm;"| Let us execute again
 +
 
 +
Come back to our terminal
  
Let us compile and execute as before
+
Compile as before, execute as before  
  
 
Yes it is working!
 
Yes it is working!
Line 380: Line 411:
  
 
'''int'''  
 
'''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
+
| 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 another common error
 +
 
 +
come back to our program
 +
 
 +
Suppose here I will type '''int''' in place of''' char'''
  
Suppose I type '''int''' here in place of''' char'''
+
'''now click on save '''
  
 
Let us see what happens
 
Let us see what happens
  
Come back on the terminal
+
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: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;"| Let us compile and execute as before
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let me clear the prompt
  
We see there is an error and a warning
+
Compile as before
  
'''The format specifier for string is %s '''
+
We see an error
  
And we are declaring it with an integer data type '''int'''
+
Wide charactr array initialized from non-wide string
 +
 
 +
format %s expects argument of type 'char'
 +
 
 +
But argument 2 has type 'int'
 +
 
 +
come back to our program
 +
 
 +
this is because we used %s as the '''format specifier for string '''
 +
 
 +
And we are initializing it with an integer data type '''int'''
  
 
|-
 
|-
Line 402: Line 447:
 
| 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
 
| 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
+
Type 'char' here
 +
 
 +
click on save
 +
 
 +
let us execute
 +
 
 +
 
  
Let us type '''char '''here
 
  
 
|-
 
|-
Line 414: Line 464:
 
| 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
 
| 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
+
Compile as before, execute as before
  
 
|-
 
|-
Line 422: Line 472:
 
|-
 
|-
 
| 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: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++
+
| 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 EXECUTE THE SAME PROGRAM IN C++
  
 
Come back to our program
 
Come back to our program
Line 435: Line 485:
  
 
'''.cpp'''
 
'''.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'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let me open our file '''string.c'''
  
Click on '''File''' menu
+
We will edit the code here
 +
 
 +
First press shift, cntrl and S key simultaneously on your keyboard
  
Click on '''Save as '''option
 
  
 
Now save the file with an extension '''.cpp'''
 
Now save the file with an extension '''.cpp'''
  
|-
+
and click on save
| 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;"| Let me change a few things here
+
  
 
|-
 
|-
Line 451: Line 500:
  
 
iostream
 
iostream
| 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
+
| 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 change the header file as
  
 
'''iostream'''
 
'''iostream'''
Line 460: Line 509:
  
 
'''<nowiki>#include <string></nowiki>'''
 
'''<nowiki>#include <string></nowiki>'''
| 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
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Include the using statement
  
Type
+
now click on save
  
'''<nowiki>#include <string></nowiki>'''
+
now we will delete this declaration
 
+
|-
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type
+
 
+
using namespace std;
+
| 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
+
  
 
|-
 
|-
Line 476: Line 519:
  
 
string strname;
 
string strname;
| 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'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| and declare a string variable, type string space strname and a semicolon
  
Since we don't use the '''format specifier''' in C++ ,
+
Click on save
 
+
the compiler should know that '''strname''' is a '''string''' variable
+
  
 
|-
 
|-
Line 486: Line 527:
  
 
<nowiki>cout <<</nowiki>
 
<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
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Replace the '''printf '''statement with '''cout''' statement
 +
 
 +
delete the closing bracket here
  
  
Line 500: Line 543:
  
  
| 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
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Delete the '''scanf''' statement and
  
 
Type
 
Type
  
'''getline(cin, strname);'''
+
'''getline(cin, strname) '''at the end type a''' ;'''
 
+
|-
+
| 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'''
+
  
 
|-
 
|-
Line 521: Line 558:
  
 
printf("The string is %s\n",strname);
 
printf("The string is %s\n",strname);
| 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
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Again replace the '''printf''' statement with '''cout''' statement
  
Delete the format specifier and '''\n''' here
+
Delete the format specifier and '''\n'''  
  
Type two opening angle brackets
+
Now delete the ''', '''
  
Then again type two opening angle brackets here and type within the double quotes '''\n'''
+
Type two opening angle brackets, delete the bracket here
  
Delete the closing bracket here
+
type two opening angle brackets and within the double quotes type '''\n'''
  
 
|-
 
|-
 
| 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: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;"| Now 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;"| And click on '''Save'''
 +
 
 +
|-
 +
| 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 have declared a string variable ''''strname''''
 +
 
 +
since we do not use the format specifier in C++
 +
 
 +
the compiler should know that strname is a string variable
 +
 
 +
Here 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;"| 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="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:1pt solid #000000;padding:0.097cm;"| Now 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="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let me clear the prompt
  
 
|-
 
|-
Line 545: Line 596:
 
| 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
 
| 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'''
+
'''g++''' space '''string.cpp '''space''' -o '''space''' str3'''
 +
 
 +
'''and press Enter'''
  
 
|-
 
|-
Line 553: Line 606:
 
| 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
 
| 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'''
+
'''./str3'''
 +
 
 +
'''Press enter'''
  
 
|-
 
|-
 
| 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: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
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''It is '''displayed as '''Enter the string'''
 +
 
 +
I will enter as '''Talk To A Teacher'''
  
I will type''' Talk To A Teacher'''
+
Now press '''Enter'''
  
 
|-
 
|-
Line 566: Line 623:
  
 
'''The string is Talk To A Teacher'''
 
'''The string is Talk To A Teacher'''
 +
 +
We can see that the output is similar to our C code
 +
 +
Now come back to our slides
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 7
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 7
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us summarize
 +
 +
In this tutorial we learnt
 +
 +
Strings
 +
 +
* Declaration of a string
 +
 +
<nowiki>eg: char strname[30]</nowiki>
 +
 +
* Initialization of a string
 +
 +
<nowiki>eg: char strname[30] = “Talk To A </nowiki>Teacher”
 +
 +
 +
 +
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 8
  
 
Assignment
 
Assignment
Line 576: Line 656:
  
 
|-
 
|-
| style="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:none;padding:0.097cm;"| Slide 9
  
  
Line 589: Line 669:
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 9
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 10
  
 
Spoken Tutorial Workshops
 
Spoken Tutorial Workshops
Line 606: Line 686:
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 10
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 11
  
  

Revision as of 12:23, 13 December 2013

Title of script: Strings

Author: Ashwini R Patil

Keywords: Strings in C, getline, Video Tutorial


Visual Cue
Narration
Slide 1


Welcome to the spoken-tutorial on Strings in C and C++
Slide 2

Learning Objectives

In this tutorial, we will learn,

What is a string.

Declaration of a string.

Initialization of a string.

Few examples on string.

We will also see some common errors and their solutions

Slide 3

System Requirements

To record this tutorial, I am using

Ubuntu Operating System version 11.04

gcc and g++ Compiler version 4.6.1

Slide 4

Introduction to strings

Let us start with the Introduction to the Strings

String is a sequence of characters that is treated as a single data item

size of string = length of string + 1

Slide 5

Declaration of the String

Syntax:

char val[10];

Let me tell you how to declare a string

The syntax for this is

char name_of_string[size];

char, name of string and size

char is the data type, name of the string is the string name, and we can give the size here.

Eg: here we have declared a character string names with size 30

Please open the Terminal


Now we will see an example

I have already typed the program

I will open it

Note that our file name is string.c

In this program we will take a string as an input from the user and print it

Let me explain the code now

Highlight

#include<string.h>

These are out header files

here string.h includes the declarations, functions, constants of string handling utilities

Whenever we work on string functions, we should include this header file

Highlight

char strname[30];

This is our main function

Here we are declaring the string 'strname' with size '30'

Highlight

printf("Enter the string\n");

Here we are accepting a string from the user
Highlight

scanf("%[^\n]s",strname);


To read a string, we can use scanf() function with format specifier %s

We are using the caret sign and \n to include the spaces with the string

Type:

printf("The string is %s\n", strname);

Then we print the string

And this is our return statement

Save the program Now click on Save
Execution of the program

On the terminal


Let us execute the program

Please open the terminal window by pressing Ctrl, Alt and T keys simultaneously on your keyboard

Type

gcc strdeclare.c str

>> press Enter

To compile

Type

gcc space string.c space -o space str

And press Enter

Type

./str

>> press Enter

To execute,

type

./str

now press Enter

Here it is displayed as Enter the string
Type

Talk To A Teacher >> press Enter


I will type

Talk To A Teacher

now press Enter

Highlight

Output

The output is displayed as

The string is Talk To A Teacher

Slide 6


Initialization of the string


Syntax:


char strinitialize[20] = “star”;


(or)


char strinitialize[] = {'S', 't', 'r'};

Now let us switch back to our slides


Until now we discussed about the declaration of a string

Now we are going to discuss how to initialize the string

The syntax for this is

char var_name[size] = “string”;

eg: here we have declared a character string names with size 30 and the string is Priya

Another syntax is

char var_name[ ] = {'S', 't', 'r', 'i', 'n', 'g'}; within single quotes

eg: char names[10] = {'P', 'r', 'i', 'y', 'a'}; in single quotes



Please switch to the Text editor


Let me show you how to use the first syntax with an example

Switch back to our Editor

We will use the same example



Click on

File >> Save as option

Type

strinitialize.c

First press shift, cntrl and s key simultaneously on your keyboard

now save the file with the name strinitialize.c

Click on save Now click Save
Type


char name[30] = "Talk To A Teacher";


We are going to intialize this string

Hence at the 5th line type

= and within double quotes “Spoken- Tutorial”;

now click on save



Delete

printf("Enter the string\n");

scanf("%[^\n]s",strname);

Now remove these two lines

as we are only going to print the string

click on save

Save the program let us execute
On the terminal

Type

gcc strinitialize.c -o str1


Come back to our terminal

To compile

Type

gcc space stringintialize.c space -o space str2

To execute

Type

./str1

Here we have str2 because we dont want to overwrite the output parameter str for the file string.c

now press Enter

To execute type ./str2

Highlight

Output

The output is displayed as

The string is Spoken-Tutorial

Error1

Type

sting.h

Now we will see some common errors which we can come across

Come back to our program

Suppose here we type the spelling of string as

sting

Click on Save Now click on Save
On the terminal Let us execute, come back to the terminal
Compile and execute as before


gcc strinitialize.c -o str1


./str1

Now compile as before

We see a fatal error

sting.h no such file or directory

compilation terminated

On the editor, correct the spelling Come back to the program

This is because the compiler is not able to find the header file with the name sting.h

Hence it is giving an error

Let us fix the error

Type r here

now click on save

On the terminal Let us execute again

Come back to our terminal

Compile as before, execute as before

Yes it is working!

Error2

Type

int

Now let us see another common error

come back to our program

Suppose here I will type int in place of char

now click on save

Let us see what happens

Come back to our terminal

On the terminal Let me clear the prompt

Compile as before

We see an error

Wide charactr array initialized from non-wide string

format %s expects argument of type 'char'

But argument 2 has type 'int'

come back to our program

this is because we used %s as the format specifier for string

And we are initializing it with an integer data type int

On the editor Let us fix the error

Type 'char' here

click on save

let us execute



Click on save Click on Save
On the terminal Come back to our terminal

Compile as before, execute as before

Yes it is working!
On the editor NOW WE WILL SEE HOW TO EXECUTE THE SAME PROGRAM IN C++

Come back to our program

Click on File


Save as

Type

.cpp

Let me open our file string.c

We will edit the code here

First press shift, cntrl and S key simultaneously on your keyboard


Now save the file with an extension .cpp

and click on save

Type

iostream

Now we will change the header file as

iostream

Type


#include <string>

Include the using statement

now click on save

now we will delete this declaration

Type

string strname;

and declare a string variable, type string space strname and a semicolon

Click on save

Type

cout <<

Replace the printf statement with cout statement

delete the closing bracket here



Delete scanf

Type

getline(cin, strname);


Delete the scanf statement and

Type

getline(cin, strname) at the end type a ;

Type

cout <<"The string is " <<strname <<"\n";


In place of

printf("The string is %s\n",strname);

Again replace the printf statement with cout statement

Delete the format specifier and \n

Now delete the ,

Type two opening angle brackets, delete the bracket here

type two opening angle brackets and within the double quotes type \n

Click on save And click on Save
we have declared a string variable 'strname'

since we do not use the format specifier in C++

the compiler should know that strname is a string variable

Here we use getline to extract the characters from the input sequence, it stores them as a string

On the terminal Now let us execute the program, Come back to our terminal
Let me clear the prompt
Type

g++ string.cpp -o str1

To compile type

g++ space string.cpp space -o space str3

and press Enter

Type

./str1

To execute type

./str3

Press enter

Highlight >> type Talk To A Teacher It is displayed as Enter the string

I will enter as Talk To A Teacher

Now press Enter

The output is displayed as

The string is Talk To A Teacher

We can see that the output is similar to our C code

Now come back to our slides

Slide 7 Let us summarize

In this tutorial we learnt

Strings

  • Declaration of a string

eg: char strname[30]

  • Initialization of a string

eg: char strname[30] = “Talk To A Teacher”



Slide 8

Assignment

As an assignment

Write a program to print a string using the 2nd syntax

Slide 9


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 10

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@spoken-tutorial.org

Slide 11


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

Ashwini Patil from IIT Bombay

Thank You for joining

Contributors and Content Editors

Ashwini, Nancyvarkey