Difference between revisions of "C-and-C++/C3/Strings/English"
Nancyvarkey (Talk | contribs) |
|||
Line 7: | Line 7: | ||
− | {| | + | {| border=1 |
− | ! | + | ! '''Visual Cue''' |
− | ! | + | ! '''Narration''' |
|- | |- | ||
− | + | |Slide 1 | |
− | + | |Welcome to the spoken-tutorial on '''Strings''' in '''C '''and''' C++''' | |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
− | + | |Slide 2 | |
Learning Objectives | Learning Objectives | ||
− | + | |In this tutorial, we will learn, | |
− | What is a string. | + | *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. | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | We will also see some common errors and their solutions | + | |
|- | |- | ||
− | + | |Slide 3 | |
System Requirements | System Requirements | ||
− | + | |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 | + | |
|- | |- | ||
− | + | |Slide 4 | |
Introduction to strings | Introduction to strings | ||
− | + | |Let us start with the introduction to '''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 | |
|- | |- | ||
− | + | |Slide 5 | |
Declaration of the String | Declaration of the String | ||
Line 62: | Line 55: | ||
<nowiki>char val[10];</nowiki> | <nowiki>char val[10];</nowiki> | ||
− | + | |Let me tell you how to declare a '''string'''. | |
The syntax for this is | The syntax for this is | ||
Line 68: | Line 61: | ||
'''<nowiki>char name_of_string[size]; </nowiki>''' | '''<nowiki>char name_of_string[size]; </nowiki>''' | ||
− | char, name of string and 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. | + | '''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 | + | '''Eg: here we have declared a character string names with size 10 ''' |
|- | |- | ||
− | + | |Please open the Terminal | |
− | + | |Now we will see an example. | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | I will open it | + | I have already typed the program, so I will open it. |
Note that our file name is '''string.c''' | 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 | + | Let me explain the code now. |
|- | |- | ||
− | + | |Highlight | |
'''<nowiki>#include<string.h></nowiki>''' | '''<nowiki>#include<string.h></nowiki>''' | ||
− | + | |These are our header files. | |
− | + | Here '''string.h''' includes the declarations, functions, constants of '''string''' handling utilities. | |
− | Whenever we work on '''string ''' | + | Whenever we work on '''string functions''', we should include this header file. |
|- | |- | ||
− | + | |Highlight | |
'''<nowiki>char strname[30]</nowiki>'''<nowiki>; </nowiki> | '''<nowiki>char strname[30]</nowiki>'''<nowiki>; </nowiki> | ||
− | + | |This is our '''main function'''. | |
− | Here we are declaring the '''string | + | Here we are declaring the '''string strname'''' with size ''''30''''. |
|- | |- | ||
− | + | |Highlight | |
'''printf("Enter the string\n"); ''' | '''printf("Enter the string\n"); ''' | ||
− | + | |Here we are accepting a '''string''' from the user. | |
|- | |- | ||
− | + | |Highlight | |
'''<nowiki>scanf("%[^\n]s",strname);</nowiki>''' | '''<nowiki>scanf("%[^\n]s",strname);</nowiki>''' | ||
+ | |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'''. | |
− | + | ||
− | + | ||
− | + | ||
− | We are using the '''caret''' sign and''' \n''' to include the spaces with the '''string''' | + | |
|- | |- | ||
− | + | |Type: | |
'''printf("The string is %s\n", strname);''' | '''printf("The string is %s\n", strname);''' | ||
− | + | |Then we print the '''string'''. | |
− | And this is our return statement | + | And this is our '''return statement'''. |
|- | |- | ||
− | + | |Save the program | |
− | + | |Now click on '''Save''' | |
|- | |- | ||
− | + | |Execution of the program | |
On the terminal | On the terminal | ||
+ | |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 | + | |
|- | |- | ||
− | + | |Type | |
'''gcc strdeclare.c str''' | '''gcc strdeclare.c str''' | ||
>> press '''Enter''' | >> press '''Enter''' | ||
− | + | |To compile, type | |
− | + | ||
− | + | ||
'''gcc '''space''' string.c '''space''' -o '''space '''str''' | '''gcc '''space''' string.c '''space''' -o '''space '''str''' | ||
Line 164: | Line 144: | ||
|- | |- | ||
− | + | |Type | |
'''./str ''' | '''./str ''' | ||
>> press '''Enter''' | >> press '''Enter''' | ||
− | + | |To execute, type | |
− | + | ||
− | type | + | |
'''./str ''' | '''./str ''' | ||
Line 178: | Line 156: | ||
|- | |- | ||
− | + | | | |
− | + | |Here it is displayed as '''Enter the string '''. | |
|- | |- | ||
− | + | |Type | |
'''Talk To A Teacher >>''' press '''Enter''' | '''Talk To A Teacher >>''' press '''Enter''' | ||
+ | |I will type '''Talk To A Teacher'''. | ||
− | + | now press '''Enter'''. | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | now press '''Enter''' | + | |
|- | |- | ||
− | + | |Highlight | |
Output | Output | ||
− | + | |The output is displayed as '''The string is Talk To A Teacher''' | |
− | + | ||
− | '''The string is Talk To A Teacher''' | + | |
|- | |- | ||
− | + | |Slide 6 | |
Line 212: | Line 183: | ||
− | <nowiki>char strinitialize[ | + | <nowiki>char strinitialize[30] = “star”;</nowiki> |
Line 219: | Line 190: | ||
<nowiki>char strinitialize[] = {'S', 't', 'r'};</nowiki> | <nowiki>char strinitialize[] = {'S', 't', 'r'};</nowiki> | ||
− | + | |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 230: | Line 201: | ||
'''<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 '''30''' and the '''string''' is '''"Priya"''' | |
Another syntax is | Another syntax is | ||
Line 237: | Line 208: | ||
'''<nowiki>eg: char names[10] = {'P', 'r', 'i', 'y', 'a'}; </nowiki>'''in single quotes | '''<nowiki>eg: char names[10] = {'P', 'r', 'i', 'y', 'a'}; </nowiki>'''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 | |
− | + | ||
− | File >> Save as option | + | |
Type | Type | ||
strinitialize.c | strinitialize.c | ||
− | + | |First, press '''shift, ctrl''' and '''s''' keys simultaneously on your keyboard | |
− | + | Now save the file with the name '''strinitialize.c ''' | |
|- | |- | ||
− | + | |Click on save | |
− | + | |Now click '''Save''' | |
|- | |- | ||
− | + | |Type | |
Line 279: | Line 237: | ||
− | + | |We are going to intialize this '''string'''. | |
− | Hence | + | Hence, on the 5th line, type |
'''<nowiki>= and within </nowiki>double quotes “Spoken- Tutorial”;''' | '''<nowiki>= and within </nowiki>double quotes “Spoken- Tutorial”;''' | ||
− | + | Now, click on '''Save''' | |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
− | + | |Delete | |
'''printf("Enter the string\n"); ''' | '''printf("Enter the string\n"); ''' | ||
'''<nowiki>scanf("%[^\n]s",strname);</nowiki>''' | '''<nowiki>scanf("%[^\n]s",strname);</nowiki>''' | ||
− | + | |Now remove these two lines, as we are only going to print the '''string'''. | |
− | + | |- | |
+ | |Save the program | ||
+ | |Click on '''Save'''. | ||
− | + | Let us execute. | |
|- | |- | ||
− | + | |On the terminal, type | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
'''gcc strinitialize.c -o str1''' | '''gcc strinitialize.c -o str1''' | ||
+ | |Come back to our '''terminal'''. | ||
+ | To compile, type | ||
− | + | '''gcc space stringintialize.c space -o space str2''' | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | '''gcc space stringintialize.c | + | |
|- | |- | ||
− | + | |To execute | |
Type | Type | ||
'''./str1''' | '''./str1''' | ||
− | + | |Here we have '''str2''' because we don't want to overwrite the output parameter '''str''' for the file '''string.c''' | |
− | + | Now press '''Enter. ''' | |
− | + | To execute, type '''./str2 ''' | |
|- | |- | ||
− | + | |Highlight | |
'''Output''' | '''Output''' | ||
− | + | |The output is displayed as '''"The string is Spoken-Tutorial".''' | |
− | + | ||
− | '''The string is Spoken-Tutorial''' | + | |
|- | |- | ||
− | + | |Error1 | |
Type | Type | ||
'''sting.h''' | '''sting.h''' | ||
− | + | |Now we will see some common errors which we can come across | |
Come back to our program | Come back to our program | ||
Line 358: | Line 303: | ||
|- | |- | ||
− | + | |Click on '''Save''' | |
− | + | |Now click on '''Save.''' | |
|- | |- | ||
− | + | |On the terminal | |
− | + | |Let us execute; come back to the '''terminal'''. | |
|- | |- | ||
− | + | |Compile and execute as before | |
Line 373: | Line 318: | ||
'''./str1''' | '''./str1''' | ||
− | + | |Now compile as before. | |
− | We see a fatal error | + | We see a fatal error. |
'''sting.h no such file or directory''' | '''sting.h no such file or directory''' | ||
− | compilation terminated | + | '''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 | + | This is because the compiler is not able to find the header file with the name '''sting.h''' |
− | Hence it is giving an error | + | Hence it is giving an error. |
− | Let us fix the error | + | Let us fix the error. |
− | Type '''r''' here | + | Type '''r''' here. |
− | + | Now click on '''Save'''. | |
|- | |- | ||
− | + | |On the terminal | |
− | + | |Let us execute again. | |
− | Come back to our terminal | + | Come back to our '''terminal'''. |
− | Compile as before, execute as before | + | Compile as before, execute as before. |
− | Yes it is working! | + | Yes, it is working! |
|- | |- | ||
− | + | |Error2 | |
Type | Type | ||
'''int''' | '''int''' | ||
− | + | |Now, let us see another common error. | |
− | + | Come back to our program. | |
− | Suppose here I will type '''int''' in place of''' char''' | + | Suppose, here, I will type '''int''' in place of''' char'''. |
− | ''' | + | Now, click on '''Save '''. |
− | Let us see what happens | + | Let us see what happens. |
− | Come back to our terminal | + | Come back to our '''terminal'''. |
|- | |- | ||
− | + | |On the terminal | |
− | + | |Let me clear the '''prompt'''. | |
− | Compile as before | + | Compile as before. |
− | We see an error | + | We see an error. |
− | Wide | + | '''Wide character array initialized from non-wide string |
format %s expects argument of type 'char' | format %s expects argument of type 'char' | ||
− | But argument 2 has type 'int' | + | 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 | + | 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 | + | 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, ctrl''' and '''S''' key simultaneously on your keyboard. | |
− | + | ||
− | ''' | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
Now save the file with an extension '''.cpp''' | Now save the file with an extension '''.cpp''' | ||
− | and click on | + | and click on '''Save'''. |
|- | |- | ||
− | + | |Type | |
iostream | iostream | ||
− | + | |Now we will change the header file as '''iostream'''. | |
− | + | ||
− | '''iostream''' | + | |
|- | |- | ||
− | + | |Type | |
'''<nowiki>#include <string></nowiki>''' | '''<nowiki>#include <string></nowiki>''' | ||
− | + | |Include the '''using statement'''. | |
− | + | Now click on '''Save'''. | |
− | + | Now we will delete this declaration. | |
|- | |- | ||
− | + | |Type | |
string strname; | string strname; | ||
− | | | + | |And declare a '''string variable.''' |
− | Click on | + | Type '''string space strname''' and a '''semicolon''' |
+ | |||
+ | Click on '''Save'''. | ||
|- | |- | ||
− | + | |Type | |
<nowiki>cout <<</nowiki> | <nowiki>cout <<</nowiki> | ||
− | + | |Replace the '''printf '''statement with '''cout''' statement. | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
+ | Delete the closing bracket here. | ||
|- | |- | ||
− | + | |Delete scanf | |
Type | Type | ||
Line 541: | Line 478: | ||
'''getline(cin, strname);''' | '''getline(cin, strname);''' | ||
+ | |Delete the '''scanf''' statement and type '''getline(cin, strname) ''' | ||
− | + | At the end, type a''' semicolon'''. | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
− | + | |Type | |
<nowiki>cout <<"The string is " <<strname <<"\n";</nowiki> | <nowiki>cout <<"The string is " <<strname <<"\n";</nowiki> | ||
Line 558: | Line 491: | ||
printf("The string is %s\n",strname); | printf("The string is %s\n",strname); | ||
− | + | |Again, replace the '''printf''' statement with '''cout''' statement. | |
− | Delete the format specifier and '''\n''' | + | Delete the '''format specifier''' and '''\n''' |
− | Now delete the ''' | + | Now delete the '''comma ''' |
− | Type two opening angle brackets, delete the bracket here | + | 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'''. | |
− | the | + | 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''' | '''g++ string.cpp -o str1''' | ||
− | + | |To compile, type | |
− | '''g++ | + | '''g++ space string.cpp space -o space str3''' |
− | ''' | + | and press '''Enter'''. |
|- | |- | ||
− | + | |Type | |
'''./str1''' | '''./str1''' | ||
− | + | |To execute, type | |
'''./str3''' | '''./str3''' | ||
− | ''' | + | Press '''Enter'''. |
|- | |- | ||
− | + | |Highlight >> type '''Talk To A Teacher''' | |
− | | | + | |It is displayed as '''Enter the string''' |
I will enter as '''Talk To A Teacher''' | I will enter as '''Talk To A Teacher''' | ||
Line 619: | Line 552: | ||
|- | |- | ||
− | + | | | |
− | + | |The output is displayed as | |
− | '''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 | + | We can see that the output is similar to our '''C''' code. |
− | Now come back to our slides | + | Now come back to our slides. |
|- | |- | ||
− | + | |Slide 7 | |
− | + | |Let us summarize | |
In this tutorial we learnt | In this tutorial we learnt | ||
− | Strings | + | *'''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” | ||
+ | ''' | ||
|- | |- | ||
− | + | |Slide 8 | |
Assignment | Assignment | ||
− | + | |As an assignment | |
− | Write a program to print a string using the 2<sup>nd</sup> syntax | + | Write a program to print a '''string''' using the 2<sup>nd</sup> syntax |
|- | |- | ||
− | + | |Slide 9 | |
About the Spoken Tutorial Project | About the Spoken Tutorial Project | ||
− | + | |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 669: | Line 599: | ||
|- | |- | ||
− | + | |Slide 10 | |
Spoken Tutorial Workshops | Spoken Tutorial Workshops | ||
Line 675: | Line 605: | ||
− | + | |The Spoken Tutorial Project Team | |
Conducts workshops using spoken tutorials | Conducts workshops using spoken tutorials | ||
Line 686: | Line 616: | ||
|- | |- | ||
− | + | |Slide 11 | |
Acknowledgement | 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 | It is supported by the National Mission on Education through ICT, MHRD, Government of India | ||
Line 697: | Line 627: | ||
|- | |- | ||
− | + | | | |
− | | | + | |This is Ashwini Patil from IIT Bombay signing off. |
− | Thank You for joining | + | Thank You for joining. |
|} | |} |
Revision as of 14:40, 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,
We will also see some common errors and their solutions. |
Slide 3
System Requirements |
To record this tutorial, I am using
|
Slide 4
Introduction to strings |
Let us start with the introduction to 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 10 |
Please open the Terminal | Now we will see an example.
I have already typed the program, so 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 our 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
|
Now let us switch back to our slides
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, ctrl and s keys simultaneously on your keyboard
Now save the file with the name strinitialize.c |
Click on save | Now click Save |
Type
|
We are going to intialize this string.
Hence, on 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. |
Save the program | Click on Save.
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 don't 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
|
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 character 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, ctrl and S key simultaneously on your keyboard.
and click on Save. |
Type
iostream |
Now we will change the header file as iostream. |
Type
|
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 semicolon. |
Type
cout <<"The string is " <<strname <<"\n";
printf("The string is %s\n",strname); |
Again, replace the printf statement with cout statement.
Delete the format specifier and \n Now delete the comma 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
|
Slide 8
Assignment |
As an assignment
Write a program to print a string using the 2nd syntax |
Slide 9
|
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
|
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 signing off.
Thank You for joining. |