Difference between revisions of "C-and-C++/C3/Strings/English-timed"
From Script | Spoken-Tutorial
PoojaMoolya (Talk | contribs) |
PoojaMoolya (Talk | contribs) |
||
(7 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
{| border = 1 | {| border = 1 | ||
− | + | |'''Time''' | |
− | |Time | + | |'''Narration''' |
− | + | ||
− | |Narration | + | |
|- | |- | ||
| 00:01 | | 00:01 | ||
− | |Welcome to the spoken-tutorial on '''Strings''' in '''C and C++''' | + | |Welcome to the spoken-tutorial on '''Strings''' in '''C and C++'''. |
|- | |- | ||
| 00:06 | | 00:06 | ||
− | |In this tutorial we will learn | + | |In this tutorial we will learn: |
|- | |- | ||
Line 27: | Line 25: | ||
|- | |- | ||
| 00:15 | | 00:15 | ||
− | | Few examples on '''string'''. | + | |Few examples on '''string'''. |
|- | |- | ||
Line 35: | Line 33: | ||
|- | |- | ||
| 00:22 | | 00:22 | ||
− | | To record this tutorial, I am using | + | | To record this tutorial, I am using |
|- | |- | ||
| 00:25 | | 00:25 | ||
− | |'''Ubuntu Operating System''' version 11.04 | + | |'''Ubuntu Operating System''' version 11.04, |
|- | |- | ||
|00:29 | |00:29 | ||
| '''gcc '''and '''g++ Compiler''' version 4.6.1 . | | '''gcc '''and '''g++ Compiler''' version 4.6.1 . | ||
− | |||
|- | |- | ||
Line 52: | Line 49: | ||
|- | |- | ||
|00:38 | |00:38 | ||
− | |'''String''' is a sequence of characters | + | |'''String''' is a sequence of characters that is treated as a single data item. |
|- | |- | ||
| 00:44 | | 00:44 | ||
− | |Size of '''string '''= length of '''string + 1 | + | |Size of '''string '''= length of '''string''' + 1. |
− | + | ||
|- | |- | ||
| 00:49 | | 00:49 | ||
− | |Let me tell you how to declare a '''string. | + | |Let me tell you how to declare a '''string.''' |
|- | |- | ||
Line 69: | Line 65: | ||
|- | |- | ||
| 00:55 | | 00:55 | ||
− | |'''char''', name of '''string''' and''' size''' | + | |'''char''', name of '''string''' and '''size'''. |
− | + | ||
|- | |- | ||
Line 78: | Line 73: | ||
|- | |- | ||
| 01:06 | | 01:06 | ||
− | |Eg: here we have declared a '''character string names''' with '''size 10''' | + | |Eg: here we have declared a '''character string names''' with '''size 10'''. |
|- | |- | ||
Line 95: | Line 90: | ||
|01:23 | |01:23 | ||
|In this program, we will take a '''string''' as an input from the user and print it. | |In this program, we will take a '''string''' as an input from the user and print it. | ||
− | |||
|- | |- | ||
|01:29 | |01:29 | ||
|Let me explain the code now. | |Let me explain the code now. | ||
− | |||
|- | |- | ||
Line 109: | Line 102: | ||
| 01:34 | | 01:34 | ||
|Here '''string.h''' includes the declarations, functions, constants of '''string''' handling utilities. | |Here '''string.h''' includes the declarations, functions, constants of '''string''' handling utilities. | ||
− | |||
|- | |- | ||
Line 117: | Line 109: | ||
|- | |- | ||
| 01:47 | | 01:47 | ||
− | |This is our '''main | + | |This is our '''main()''' function. |
|- | |- | ||
|01:49 | |01:49 | ||
− | |Here we are declaring the '''string strname '''with size | + | |Here we are declaring the '''string strname '''with size ''' 30. ''' |
|- | |- | ||
Line 129: | Line 121: | ||
|- | |- | ||
|01:58 | |01:58 | ||
− | |To read a '''string''', we can use '''scanf() | + | |To read a '''string''', we can use '''scanf()''' function with format specifier '''%s'''. |
|- | |- | ||
Line 141: | Line 133: | ||
|- | |- | ||
| 02:13 | | 02:13 | ||
− | | And this is our '''return | + | | And this is our '''return''' statement. |
|- | |- | ||
| 02:16 | | 02:16 | ||
− | | Now click on '''Save''' | + | | Now click on '''Save'''. |
|- | |- | ||
Line 153: | Line 145: | ||
|- | |- | ||
| 02:20 | | 02:20 | ||
− | | 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 162: | Line 153: | ||
|- | |- | ||
| 02:37 | | 02:37 | ||
− | |And press '''Enter ''' | + | |And press '''Enter '''. |
− | + | ||
|- | |- | ||
| 02:40 | | 02:40 | ||
− | | To execute, type '''./str ''' | + | | To execute, type '''./str ''' (dot slash str). |
− | + | ||
|- | |- | ||
| 02:43 | | 02:43 | ||
− | | | + | |Now press '''Enter'''. |
− | + | ||
|- | |- | ||
Line 181: | Line 169: | ||
|- | |- | ||
| 02:49 | | 02:49 | ||
− | | I will type | + | | I will type "Talk To A Teacher". |
|- | |- | ||
| 02:56 | | 02:56 | ||
− | | | + | |Now press '''Enter'''. |
|- | |- | ||
| 02:58 | | 02:58 | ||
− | |The output is displayed as ''' The string is Talk To A Teacher''' | + | |The output is displayed as ''' The string is Talk To A Teacher.''' |
|- | |- | ||
| 03:03 | | 03:03 | ||
− | |Now let us switch back to our slides | + | |Now let us switch back to our slides. |
− | + | ||
|- | |- | ||
| 03:06 | | 03:06 | ||
|Until now we discussed about the declaration of a '''string. ''' | |Until now we discussed about the declaration of a '''string. ''' | ||
− | |||
|- | |- | ||
Line 207: | Line 193: | ||
|- | |- | ||
| 03:13 | | 03:13 | ||
− | |The syntax for this is | + | |The syntax for this is, |
|- | |- | ||
Line 215: | Line 201: | ||
|- | |- | ||
| 03:20 | | 03:20 | ||
− | |Eg: Here we have declared a '''character string “names” | + | |Eg: Here we have declared a '''character string “names”''' with size '''10''' and the '''string''' is “Priya”" |
|- | |- | ||
| 03:28 | | 03:28 | ||
− | |Another syntax is | + | |Another syntax is, |
|- | |- | ||
| 03:31 | | 03:31 | ||
− | |'''char var_name[ ] = {'S', 't', 'r', 'i', 'n', 'g'}''' within single quotes | + | |'''char var_name[ ] = {'S', 't', 'r', 'i', 'n', 'g'}''' within single quotes. |
− | + | ||
|- | |- | ||
| 03:36 | | 03:36 | ||
− | |eg: '''char names[10] = {'P', 'r', 'i', 'y', 'a'}''' in single quotes | + | |eg: '''char names[10] = {'P', 'r', 'i', 'y', 'a'}''' in single quotes. |
|- | |- | ||
Line 237: | Line 222: | ||
| 03:48 | | 03:48 | ||
|Switch back to our Editor. We will use the same example. | |Switch back to our Editor. We will use the same example. | ||
− | |||
|- | |- | ||
| 03:52 | | 03:52 | ||
− | |First, press '''shift, ctrl and s '''keys simultaneously on your keyboard | + | |First, press '''shift, ctrl''' and '''s '''keys simultaneously on your keyboard. |
|- | |- | ||
| 03:58 | | 03:58 | ||
− | |Now save the file with the name stringinitialize''' | + | |Now save the file with the name '''stringinitialize'''. |
|- | |- | ||
| 04:03 | | 04:03 | ||
− | |Now click on '''Save''' | + | |Now click on '''Save'''. |
− | + | ||
|- | |- | ||
| 04:06 | | 04:06 | ||
| We are going to initialize the '''string.''' | | We are going to initialize the '''string.''' | ||
− | |||
|- | |- | ||
Line 263: | Line 245: | ||
|- | |- | ||
| 04:11 | | 04:11 | ||
− | | '''= and within the double quotes “Spoken- Tutorial”; ''' | + | | '''= and within the double quotes “Spoken-Tutorial”; ''' |
|- | |- | ||
| 04:20 | | 04:20 | ||
− | |Now, click on '''Save ''' | + | |Now, click on '''Save '''. |
|- | |- | ||
Line 279: | Line 261: | ||
|- | |- | ||
| 04:30 | | 04:30 | ||
− | |Let us execute. | + | |Let us execute. Come back to our '''terminal.''' |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
Line 292: | Line 270: | ||
| 04:35 | | 04:35 | ||
|'''gcc space stringinitialize.c space -o space str2 ''' | |'''gcc space stringinitialize.c space -o space str2 ''' | ||
− | |||
|- | |- | ||
| 04:44 | | 04:44 | ||
− | |Here we have '''str2''' because we don't want to overwrite the output parameter str for the file string.c | + | |Here we have '''str2''' because we don't want to overwrite the output parameter '''str''' for the file string.c. |
|- | |- | ||
Line 316: | Line 293: | ||
|- | |- | ||
| 05:09 | | 05:09 | ||
− | |Come back to our program | + | |Come back to our program. |
|- | |- | ||
| 05:11 | | 05:11 | ||
− | |Suppose here we type the spelling of string as '''sting''' | + | |Suppose, here we type the spelling of string as '''sting'''. |
|- | |- | ||
| 05:16 | | 05:16 | ||
|Now click on '''Save.''' | |Now click on '''Save.''' | ||
− | |||
|- | |- | ||
| 05:18 | | 05:18 | ||
− | |Let us execute | + | |Let us execute.Come back to our '''terminal'''. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| 05:21 | | 05:21 | ||
− | |Now compile as before | + | |Now compile as before. |
|- | |- | ||
| 05:23 | | 05:23 | ||
|We see an fatal error. | |We see an fatal error. | ||
− | |||
|- | |- | ||
| 05:25 | | 05:25 | ||
− | |'''sting.h: | + | |'''sting.h: No such file or directory ''' |
− | + | ||
|- | |- | ||
| 05:28 | | 05:28 | ||
− | |'''compilation terminated ''' | + | |'''compilation terminated. ''' |
|- | |- | ||
Line 359: | Line 329: | ||
|- | |- | ||
| 05:32 | | 05:32 | ||
− | |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 '''. |
− | + | ||
|- | |- | ||
| 05:39 | | 05:39 | ||
|Hence it is giving an error. | |Hence it is giving an error. | ||
− | |||
|- | |- | ||
Line 374: | Line 342: | ||
| 05:43 | | 05:43 | ||
|Type '''r''' here. | |Type '''r''' here. | ||
− | |||
|- | |- | ||
| 05:45 | | 05:45 | ||
− | |Now click on '''Save. ''' | + | |Now click on '''Save. '''Let us execute again. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| 05:47 | | 05:47 | ||
|Come back to our '''terminal. ''' | |Come back to our '''terminal. ''' | ||
− | |||
|- | |- | ||
Line 396: | Line 358: | ||
| 05:54 | | 05:54 | ||
|Yes, it is working! | |Yes, it is working! | ||
− | |||
|- | |- | ||
Line 405: | Line 366: | ||
| 05:59 | | 05:59 | ||
|Come back to our program. | |Come back to our program. | ||
− | |||
|- | |- | ||
| 06:02 | | 06:02 | ||
− | |Suppose, here | + | |Suppose, here I will type '''int''' in place of '''char'''. |
− | + | ||
|- | |- | ||
| 06:06 | | 06:06 | ||
− | |Now, click on Save . | + | |Now, click on Save .Let us see what happens. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| 06:09 | | 06:09 | ||
|Come back to our terminal. | |Come back to our terminal. | ||
− | |||
|- | |- | ||
Line 440: | Line 393: | ||
|- | |- | ||
| 06:19 | | 06:19 | ||
− | |'''Wide character array initialized from non-wide string ''' | + | |'''Wide character array initialized from non-wide string '''. |
|- | |- | ||
| 06:24 | | 06:24 | ||
− | |format %s expects argument of type 'char | + | |'''format %s expects argument of type 'char' but argument 2 has type 'int' '''. |
|- | |- | ||
Line 456: | Line 409: | ||
|- | |- | ||
| 06:42 | | 06:42 | ||
− | |And we are initializing it with an '''integer''' data type | + | |And we are initializing it with an '''integer''' data type. |
− | + | ||
|- | |- | ||
Line 466: | Line 418: | ||
| 06:49 | | 06:49 | ||
|Type '''char''' here. | |Type '''char''' here. | ||
− | |||
|- | |- | ||
Line 480: | Line 431: | ||
|Compile as before, execute as before. | |Compile as before, execute as before. | ||
− | |||
|- | |- | ||
| 07:00 | | 07:00 | ||
− | |Yes it is working! | + | |Yes, it is working! |
|- | |- | ||
| 07:03 | | 07:03 | ||
− | |Now we will see how to execute the same program in '''C++''' | + | |Now we will see how to execute the same program in '''C++'''. |
|- | |- | ||
| 07:08 | | 07:08 | ||
|Come back to our program. | |Come back to our program. | ||
− | |||
|- | |- | ||
| 07:11 | | 07:11 | ||
− | |Let me open our file '''string.c''' | + | |Let me open our file '''string.c'''. |
|- | |- | ||
Line 504: | Line 453: | ||
|- | |- | ||
| 07:18 | | 07:18 | ||
− | |First, press '''shift, ctrl and S''' | + | |First, press '''shift, ctrl''' and '''S''' keys simultaneously on your keyboard. |
|- | |- | ||
| 07:25 | | 07:25 | ||
− | |Now save the file with an extension '''.cpp''' | + | |Now save the file with an extension '''.cpp''' (dot cpp). |
|- | |- | ||
Line 525: | Line 474: | ||
| 07:43 | | 07:43 | ||
|Now click on '''Save.''' | |Now click on '''Save.''' | ||
− | |||
|- | |- | ||
Line 531: | Line 479: | ||
|Now we will delete this declaration. | |Now we will delete this declaration. | ||
− | |||
|- | |- | ||
| 07:50 | | 07:50 | ||
Line 538: | Line 485: | ||
|- | |- | ||
| 07:53 | | 07:53 | ||
− | |Type '''string space strname''' and a '''semicolon''' | + | |Type '''string space strname''' and a '''semicolon'''. |
|- | |- | ||
| 07:59 | | 07:59 | ||
|Click on '''Save.''' | |Click on '''Save.''' | ||
− | |||
|- | |- | ||
Line 555: | Line 501: | ||
|- | |- | ||
| 08:11 | | 08:11 | ||
− | |Delete the '''scanf''' statement and type '''getline opening bracket closing bracket within the brackets type(cin, strname) ''' | + | |Delete the '''scanf''' statement and type '''getline opening bracket closing bracket''', within the brackets type '''(cin, strname) '''. |
|- | |- | ||
Line 567: | Line 513: | ||
|- | |- | ||
| 08:36 | | 08:36 | ||
− | |Delete the '''format specifier''' and '''\n ''' | + | |Delete the '''format specifier''' and '''\n '''. |
− | + | ||
|- | |- | ||
| 08:40 | | 08:40 | ||
− | |Now delete the '''comma''' | + | |Now delete the '''comma'''. |
− | + | ||
|- | |- | ||
| 08:42 | | 08:42 | ||
|Type two opening angle brackets, delete the bracket here. | |Type two opening angle brackets, delete the bracket here. | ||
− | |||
|- | |- | ||
| 08:49 | | 08:49 | ||
− | |Type two opening angle brackets and within the double quotes type''' \n ''' | + | |Type two opening angle brackets and within the double quotes type''' \n '''. |
|- | |- | ||
| 08:54 | | 08:54 | ||
− | |And click on '''Save''' | + | |And click on '''Save'''. |
|- | |- | ||
| 08:58 | | 08:58 | ||
− | |Here we have declared a '''string variable 'strname' ''' | + | |Here we have declared a '''string variable 'strname' '''. |
|- | |- | ||
| 09:03 | | 09:03 | ||
− | |Since we do not use the '''format specifier''' in '''C++''', the compiler should know that strname is a '''string variable. ''' | + | |Since we do not use the '''format specifier''' in '''C++''', the compiler should know that 'strname' is a '''string variable. ''' |
|- | |- | ||
Line 603: | Line 546: | ||
| 09:18 | | 09:18 | ||
|It stores them as a '''string'''. | |It stores them as a '''string'''. | ||
− | |||
|- | |- | ||
| 09:22 | | 09:22 | ||
|Now let us execute the program. Come back to our terminal. | |Now let us execute the program. Come back to our terminal. | ||
− | |||
|- | |- | ||
| 09:27 | | 09:27 | ||
|Let me clear the prompt. | |Let me clear the prompt. | ||
+ | |||
|- | |- | ||
| 09:30 | | 09:30 | ||
Line 620: | Line 562: | ||
| 09:32 | | 09:32 | ||
|'''g++ space string.cpp space -o space str3 ''' | |'''g++ space string.cpp space -o space str3 ''' | ||
− | |||
|- | |- | ||
| 09:39 | | 09:39 | ||
|and press '''Enter. ''' | |and press '''Enter. ''' | ||
− | |||
|- | |- | ||
| 09:41 | | 09:41 | ||
− | |To execute, type '''./str3 ''' | + | |To execute, type '''./str3 '''(dot slash str3). |
− | + | ||
|- | |- | ||
| 09:46 | | 09:46 | ||
− | |Press '''Enter. ''' | + | |Press '''Enter. '''It is displayed as '''Enter the string'''. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| 09:50 | | 09:50 | ||
− | |I will enter as '''Talk To A Teacher ''' | + | |I will enter as '''Talk To A Teacher '''. |
− | + | ||
|- | |- | ||
| 09:55 | | 09:55 | ||
− | |Now press '''Enter''' | + | |Now press '''Enter'''. |
|- | |- | ||
Line 656: | Line 589: | ||
|- | |- | ||
| 09:59 | | 09:59 | ||
− | | | + | | "The string is Talk To A Teacher" |
− | + | ||
|- | |- | ||
Line 665: | Line 597: | ||
|- | |- | ||
| 10:07 | | 10:07 | ||
− | |Now come back to our slides. | + | |Now come back to our slides. Let us summarize. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| 10:11 | | 10:11 | ||
− | |In this tutorial we learnt | + | |In this tutorial we learnt: '''Strings ''' |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | |||
|- | |- | ||
| 10:14 | | 10:14 | ||
|Declaration of a '''string ''' | |Declaration of a '''string ''' | ||
− | |||
|- | |- | ||
Line 693: | Line 613: | ||
|- | |- | ||
|10:20 | |10:20 | ||
− | |Initialization of a '''string ''' | + | |Initialization of a '''string '''eg: '''char strname[30] = “Talk To A Teacher”''' |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| 10:26 | | 10:26 | ||
− | |As an assignment | + | |As an assignment, |
|- | |- | ||
| 10:28 | | 10:28 | ||
− | | | + | |write a program to print a '''string''' using the 2nd syntax. |
|- | |- | ||
| 10:34 | | 10:34 | ||
− | |Watch the video available at the link shown below | + | |Watch the video available at the link shown below. |
|- | |- | ||
| 10:37 | | 10:37 | ||
− | |It summarizes the Spoken Tutorial project | + | |It summarizes the Spoken Tutorial project. |
|- | |- | ||
| 10:40 | | 10:40 | ||
− | |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. |
|- | |- | ||
|10:44 | |10:44 | ||
− | |The Spoken Tutorial Project Team | + | |The Spoken Tutorial Project Team: |
|- | |- | ||
| 10:46 | | 10:46 | ||
− | |Conducts workshops using spoken tutorials | + | |Conducts workshops using spoken tutorials. |
|- | |- | ||
| 10:49 | | 10:49 | ||
− | |Gives certificates to those who pass an online test | + | |Gives certificates to those who pass an online test. |
|- | |- | ||
| 10:54 | | 10:54 | ||
− | |For more details, please write to, contact@spoken-tutorial.org | + | |For more details, please write to, contact@spoken-tutorial.org. |
|- | |- | ||
|11:01 | |11:01 | ||
− | |Spoken Tutorial Project is a part of Talk to a Teacher project | + | |Spoken Tutorial Project is a part of Talk to a Teacher project. |
|- | |- | ||
|11:04 | |11:04 | ||
− | |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. |
− | + | ||
|- | |- | ||
| 11:12 | | 11:12 | ||
− | |More information on this | + | |More information on this mission is available at the link shown below. |
|- | |- | ||
| 11:16 | | 11:16 | ||
− | |This is Ashwini Patil from IIT Bombay signing off. | + | |This is Ashwini Patil from IIT Bombay, signing off. |
|- | |- | ||
| 11:20 | | 11:20 | ||
|Thank You for watching. | |Thank You for watching. |
Latest revision as of 12:43, 6 September 2017
Time | Narration |
00:01 | Welcome to the spoken-tutorial on Strings in C and C++. |
00:06 | In this tutorial we will learn: |
00:08 | What is a string. |
00:10 | Declaration of a string. |
00:13 | Initialization of a string. |
00:15 | Few examples on string. |
00:17 | We will also see some common errors and their solutions. |
00:22 | To record this tutorial, I am using |
00:25 | Ubuntu Operating System version 11.04, |
00:29 | gcc and g++ Compiler version 4.6.1 . |
00:35 | Let us start with the introduction to strings. |
00:38 | String is a sequence of characters that is treated as a single data item. |
00:44 | Size of string = length of string + 1. |
00:49 | Let me tell you how to declare a string. |
00:52 | The syntax for this is |
00:55 | char, name of string and size. |
00:59 | char is the data type, name of the string is the string name, and we can give the size here. |
01:06 | Eg: here we have declared a character string names with size 10. |
01:13 | Now we will see an example. |
01:15 | I have already typed the program, I will open it. |
01:19 | Note that our file name is string.c |
01:23 | In this program, we will take a string as an input from the user and print it. |
01:29 | Let me explain the code now. |
01:32 | These are our header files. |
01:34 | Here string.h includes the declarations, functions, constants of string handling utilities. |
01:43 | Whenever we work on string functions, we should include this header file. |
01:47 | This is our main() function. |
01:49 | Here we are declaring the string strname with size 30. |
01:55 | Here we are accepting a string from the user. |
01:58 | To read a string, we can use scanf() function with format specifier %s. |
02:05 | We are using the caret sign and \n to include the spaces with the string. |
02:11 | Then we print the string. |
02:13 | And this is our return statement. |
02:16 | Now click on Save. |
02:18 | Let us execute the program. |
02:20 | Please open the terminal window by pressing Ctrl, Alt and T keys simultaneously on your keyboard. |
02:30 | To compile, type gcc space string.c space -o space str |
02:37 | And press Enter . |
02:40 | To execute, type ./str (dot slash str). |
02:43 | Now press Enter. |
02:46 | Here it is displayed as Enter the string . |
02:49 | I will type "Talk To A Teacher". |
02:56 | Now press Enter. |
02:58 | The output is displayed as The string is Talk To A Teacher. |
03:03 | Now let us switch back to our slides. |
03:06 | Until now we discussed about the declaration of a string. |
03:10 | Now we are going to discuss how to initialize the string. |
03:13 | The syntax for this is, |
03:16 | char var_name[size] = “string”; |
03:20 | Eg: Here we have declared a character string “names” with size 10 and the string is “Priya”" |
03:28 | Another syntax is, |
03:31 | char var_name[ ] = {'S', 't', 'r', 'i', 'n', 'g'} within single quotes. |
03:36 | eg: char names[10] = {'P', 'r', 'i', 'y', 'a'} in single quotes. |
03:42 | Let me show you how to use the first syntax with an example. |
03:48 | Switch back to our Editor. We will use the same example. |
03:52 | First, press shift, ctrl and s keys simultaneously on your keyboard. |
03:58 | Now save the file with the name stringinitialize. |
04:03 | Now click on Save. |
04:06 | We are going to initialize the string. |
04:08 | Hence, at the 5th line, type |
04:11 | = and within the double quotes “Spoken-Tutorial”; |
04:20 | Now, click on Save . |
04:22 | Now remove these two lines, as we are only going to print the string. |
04:27 | Click on Save. |
04:30 | Let us execute. Come back to our terminal. |
04:33 | To compile, type |
04:35 | gcc space stringinitialize.c space -o space str2 |
04:44 | Here we have str2 because we don't want to overwrite the output parameter str for the file string.c. |
04:54 | Now press Enter. |
04:56 | To execute, type ./str2 |
05:00 | The output is displayed as "The string is Spoken-Tutorial". |
05:06 | Now we will see some common errors which we can come across . |
05:09 | Come back to our program. |
05:11 | Suppose, here we type the spelling of string as sting. |
05:16 | Now click on Save. |
05:18 | Let us execute.Come back to our terminal. |
05:21 | Now compile as before. |
05:23 | We see an fatal error. |
05:25 | sting.h: No such file or directory |
05:28 | compilation terminated. |
05:30 | Come back to our program. |
05:32 | This is because the compiler is not able to find the header file with the name sting.h . |
05:39 | Hence it is giving an error. |
05:41 | Let us fix the error. |
05:43 | Type r here. |
05:45 | Now click on Save. Let us execute again. |
05:47 | Come back to our terminal. |
05:50 | Compile as before, execute as before. |
05:54 | Yes, it is working! |
05:56 | Now, let us see another common error. |
05:59 | Come back to our program. |
06:02 | Suppose, here I will type int in place of char. |
06:06 | Now, click on Save .Let us see what happens. |
06:09 | Come back to our terminal. |
06:11 | Let me clear the prompt. |
06:15 | Compile as before. |
06:17 | We see an error. |
06:19 | Wide character array initialized from non-wide string . |
06:24 | format %s expects argument of type 'char' but argument 2 has type 'int' . |
06:32 | Come back to our program. |
06:36 | This is because we used %s as the format specifier for string. |
06:42 | And we are initializing it with an integer data type. |
06:47 | Let us fix the error. |
06:49 | Type char here. |
06:51 | Click on Save. |
06:53 | Let us execute. Come back to our terminal. |
06:56 | Compile as before, execute as before. |
07:00 | Yes, it is working! |
07:03 | Now we will see how to execute the same program in C++. |
07:08 | Come back to our program. |
07:11 | Let me open our file string.c. |
07:15 | We will edit the code here. |
07:18 | First, press shift, ctrl and S keys simultaneously on your keyboard. |
07:25 | Now save the file with an extension .cpp (dot cpp). |
07:29 | and click on Save. |
07:33 | Now we will change the header file as iostream. |
07:38 | Include the using statement. |
07:43 | Now click on Save. |
07:47 | Now we will delete this declaration. |
07:50 | And will declare a string variable. |
07:53 | Type string space strname and a semicolon. |
07:59 | Click on Save. |
08:02 | Replace the printf statement with the cout statement. |
08:07 | Delete the closing bracket here. |
08:11 | Delete the scanf statement and type getline opening bracket closing bracket, within the brackets type (cin, strname) . |
08:24 | At the end, type a semicolon. |
08:28 | Now again, replace the printf statement with the cout statement. |
08:36 | Delete the format specifier and \n . |
08:40 | Now delete the comma. |
08:42 | Type two opening angle brackets, delete the bracket here. |
08:49 | Type two opening angle brackets and within the double quotes type \n . |
08:54 | And click on Save. |
08:58 | Here we have declared a string variable 'strname' . |
09:03 | Since we do not use the format specifier in C++, the compiler should know that 'strname' is a string variable. |
09:13 | Here we use getline to extract the characters from the input sequence. |
09:18 | It stores them as a string. |
09:22 | Now let us execute the program. Come back to our terminal. |
09:27 | Let me clear the prompt. |
09:30 | To compile, type |
09:32 | g++ space string.cpp space -o space str3 |
09:39 | and press Enter. |
09:41 | To execute, type ./str3 (dot slash str3). |
09:46 | Press Enter. It is displayed as Enter the string. |
09:50 | I will enter as Talk To A Teacher . |
09:55 | Now press Enter. |
09:57 | The output is displayed as |
09:59 | "The string is Talk To A Teacher" |
10:03 | We can see that the output is similar to our C code. |
10:07 | Now come back to our slides. Let us summarize. |
10:11 | In this tutorial we learnt: Strings |
10:14 | Declaration of a string |
10:16 | eg: char strname[30] |
10:20 | Initialization of a string eg: char strname[30] = “Talk To A Teacher” |
10:26 | As an assignment, |
10:28 | write a program to print a string using the 2nd syntax. |
10:34 | Watch the video available at the link shown below. |
10:37 | It summarizes the Spoken Tutorial project. |
10:40 | If you do not have good bandwidth, you can download and watch it. |
10:44 | The Spoken Tutorial Project Team: |
10:46 | Conducts workshops using spoken tutorials. |
10:49 | Gives certificates to those who pass an online test. |
10:54 | For more details, please write to, contact@spoken-tutorial.org. |
11:01 | Spoken Tutorial Project is a part of Talk to a Teacher project. |
11:04 | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
11:12 | More information on this mission is available at the link shown below. |
11:16 | This is Ashwini Patil from IIT Bombay, signing off. |
11:20 | Thank You for watching. |