Difference between revisions of "C-and-C++/C3/Strings/English-timed"
From Script | Spoken-Tutorial
Kavita salve (Talk | contribs) |
PoojaMoolya (Talk | contribs) |
||
(9 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
{| border = 1 | {| border = 1 | ||
− | |||
|'''Time''' | |'''Time''' | ||
− | |||
|'''Narration''' | |'''Narration''' | ||
− | |||
|- | |- | ||
− | | 00 | + | | 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 | + | | 00:06 |
− | |In this tutorial we will learn | + | |In this tutorial we will learn: |
|- | |- | ||
− | | 00 | + | | 00:08 |
− | |What is a string. | + | |What is a '''string. ''' |
|- | |- | ||
− | | 00 | + | | 00:10 |
− | |Declaration of a string. | + | |Declaration of a '''string. ''' |
|- | |- | ||
− | | 00 | + | | 00:13 |
− | |Initialization of a string. | + | |Initialization of a '''string. ''' |
|- | |- | ||
− | | 00 | + | | 00:15 |
− | | Few examples on string. | + | |Few examples on '''string'''. |
|- | |- | ||
− | | 00 | + | | 00:17 |
|We will also see some common errors and their solutions. | |We will also see some common errors and their solutions. | ||
|- | |- | ||
− | | 00 | + | | 00:22 |
− | | To record this tutorial, I am using | + | | To record this tutorial, I am using |
|- | |- | ||
− | | 00 | + | | 00:25 |
− | |Ubuntu Operating System version 11.04 | + | |'''Ubuntu Operating System''' version 11.04, |
|- | |- | ||
− | |00 | + | |00:29 |
− | | gcc and g++ Compiler version 4.6.1 . | + | | '''gcc '''and '''g++ Compiler''' version 4.6.1 . |
− | + | ||
|- | |- | ||
− | |00 | + | |00:35 |
− | |Let us start with the introduction to strings. | + | |Let us start with the introduction to '''strings.''' |
|- | |- | ||
− | |00 | + | |00:38 |
− | |String is a sequence of characters | + | |'''String''' is a sequence of characters that is treated as a single data item. |
|- | |- | ||
− | | 00 | + | | 00:44 |
− | |Size of string = length of string + 1 | + | |Size of '''string '''= length of '''string''' + 1. |
− | + | ||
|- | |- | ||
− | | 00 | + | | 00:49 |
− | |Let me tell you how to declare a string. | + | |Let me tell you how to declare a '''string.''' |
|- | |- | ||
− | | 00 | + | | 00:52 |
|The syntax for this is | |The syntax for this is | ||
|- | |- | ||
− | | 00 | + | | 00:55 |
− | |char, name of string and size | + | |'''char''', name of '''string''' and '''size'''. |
− | + | ||
|- | |- | ||
− | |00 | + | |00:59 |
− | |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. |
|- | |- | ||
− | | 01 | + | | 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'''. |
|- | |- | ||
− | | 01 | + | | 01:13 |
|Now we will see an example. | |Now we will see an example. | ||
|- | |- | ||
− | | 01 | + | | 01:15 |
|I have already typed the program, I will open it. | |I have already typed the program, I will open it. | ||
|- | |- | ||
− | |01 | + | |01:19 |
− | |Note that our file name is string.c | + | |Note that our file name is '''string.c''' |
|- | |- | ||
− | |01 | + | |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 | + | |01:29 |
|Let me explain the code now. | |Let me explain the code now. | ||
− | |||
|- | |- | ||
− | |01 | + | |01:32 |
|These are our header files. | |These are our header files. | ||
|- | |- | ||
− | | 01 | + | | 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. |
− | + | ||
|- | |- | ||
− | | 01 | + | | 01:43 |
− | |Whenever we work on string functions, we should include this header file. | + | |Whenever we work on '''string functions''', we should include this header file. |
|- | |- | ||
− | | 01 | + | | 01:47 |
− | |This is our main function. | + | |This is our '''main()''' function. |
|- | |- | ||
− | |01 | + | |01:49 |
− | |Here we are declaring the string strname' with size ' | + | |Here we are declaring the '''string strname '''with size ''' 30. ''' |
|- | |- | ||
− | |01 | + | |01:55 |
− | |Here we are accepting a string from the user. | + | |Here we are accepting a '''string''' from the user. |
|- | |- | ||
− | |01 | + | |01:58 |
− | |To read a string, we can use scanf() function with format specifier %s | + | |To read a '''string''', we can use '''scanf()''' function with format specifier '''%s'''. |
|- | |- | ||
− | |02 | + | |02:05 |
|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. | ||
|- | |- | ||
− | | 02 | + | | 02:11 |
− | |Then we print the string. | + | |Then we print the '''string. ''' |
− | + | ||
|- | |- | ||
− | | 02 | + | | 02:13 |
− | | And this is our return statement. | + | | And this is our '''return''' statement. |
− | + | ||
|- | |- | ||
− | | 02 | + | | 02:16 |
− | | Now click on Save | + | | Now click on '''Save'''. |
− | + | ||
|- | |- | ||
− | | 02 | + | | 02:18 |
| Let us execute the program. | | Let us execute the program. | ||
|- | |- | ||
− | | 02 | + | | 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. |
− | + | ||
|- | |- | ||
− | | 02 | + | | 02:30 |
− | |To compile, type gcc space string.c space -o space str | + | |To compile, type '''gcc''' space '''string.c''' space '''-o''' space''' str ''' |
|- | |- | ||
− | | 02 | + | | 02:37 |
− | |And press Enter | + | |And press '''Enter '''. |
− | + | ||
|- | |- | ||
− | | 02 | + | | 02:40 |
− | | To execute, type ./str | + | | To execute, type '''./str ''' (dot slash str). |
− | + | ||
|- | |- | ||
− | | 02 | + | | 02:43 |
− | | | + | |Now press '''Enter'''. |
− | + | ||
|- | |- | ||
− | | 02 | + | | 02:46 |
− | |Here it is displayed as Enter the string . | + | |Here it is displayed as '''Enter the string . ''' |
|- | |- | ||
− | | 02 | + | | 02:49 |
− | | I will type Talk To A Teacher. | + | | I will type "Talk To A Teacher". |
|- | |- | ||
− | | 02 | + | | 02:56 |
− | | | + | |Now press '''Enter'''. |
|- | |- | ||
− | | 02 | + | | 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 |
− | |Now let us switch back to our slides | + | |Now let us switch back to our slides. |
− | + | ||
|- | |- | ||
− | | 03 | + | | 03:06 |
− | |Until now we discussed about the declaration of a string. | + | |Until now we discussed about the declaration of a '''string. ''' |
− | + | ||
|- | |- | ||
− | | 03 | + | | 03:10 |
|Now we are going to discuss how to initialize the string. | |Now we are going to discuss how to initialize the string. | ||
|- | |- | ||
− | | 03 | + | | 03:13 |
− | |The syntax for this is | + | |The syntax for this is, |
|- | |- | ||
− | | 03 | + | | 03:16 |
− | |char var_name[size] = “string”; | + | |'''char var_name[size] = “string”; ''' |
|- | |- | ||
− | | 03 | + | | 03:20 |
− | |Eg: Here we have declared a character string | + | |Eg: Here we have declared a '''character string “names”''' with size '''10''' and the '''string''' is “Priya”" |
|- | |- | ||
− | | 03 | + | | 03:28 |
− | |Another syntax is | + | |Another syntax is, |
|- | |- | ||
− | | 03 | + | | 03:31 |
− | |char var_name[ ] = | + | |'''char var_name[ ] = {'S', 't', 'r', 'i', 'n', 'g'}''' within single quotes. |
− | + | ||
|- | |- | ||
− | | 03 | + | | 03:36 |
− | |eg: char names[10] = | + | |eg: '''char names[10] = {'P', 'r', 'i', 'y', 'a'}''' in single quotes. |
− | + | ||
− | + | ||
|- | |- | ||
− | | 03 | + | | 03:42 |
|Let me show you how to use the first syntax with an example. | |Let me show you how to use the first syntax with an example. | ||
|- | |- | ||
− | | 03 | + | | 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 | + | | 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 | + | | 03:58 |
− | |Now save the file with the name stringinitialize | + | |Now save the file with the name '''stringinitialize'''. |
|- | |- | ||
− | | 04 | + | | 04:03 |
− | |Now click on Save | + | |Now click on '''Save'''. |
− | + | ||
|- | |- | ||
− | | 04 | + | | 04:06 |
− | | We are going to initialize the string. | + | | We are going to initialize the '''string.''' |
− | + | ||
|- | |- | ||
− | | 04 | + | | 04:08 |
|Hence, at the 5th line, type | |Hence, at the 5th line, type | ||
|- | |- | ||
− | | 04 | + | | 04:11 |
− | | = and within the double quotes “Spoken- Tutorial”; | + | | '''= and within the double quotes “Spoken-Tutorial”; ''' |
− | + | ||
− | + | ||
|- | |- | ||
− | | 04 | + | | 04:20 |
− | |Now, click on Save | + | |Now, click on '''Save '''. |
− | + | ||
|- | |- | ||
− | | 04 | + | | 04:22 |
− | |Now remove these two lines, as we are only going to print the string. | + | |Now remove these two lines, as we are only going to print the '''string.''' |
|- | |- | ||
− | | 04 | + | | 04:27 |
− | |Click on Save. | + | |Click on '''Save. ''' |
|- | |- | ||
− | | 04 | + | | 04:30 |
− | |Let us execute. | + | |Let us execute. Come back to our '''terminal.''' |
− | + | ||
|- | |- | ||
− | | 04 | + | | 04:33 |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|To compile, type | |To compile, type | ||
− | |||
|- | |- | ||
− | | 04 | + | | 04:35 |
− | |gcc space stringinitialize.c space -o space str2 | + | |'''gcc space stringinitialize.c space -o space str2 ''' |
− | + | ||
|- | |- | ||
− | | 04 | + | | 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. |
|- | |- | ||
− | | 04 | + | | 04:54 |
− | |Now press Enter. | + | |Now press '''Enter. ''' |
|- | |- | ||
− | | 04 | + | | 04:56 |
− | |To execute, type ./str2 | + | |To execute, type '''./str2 ''' |
|- | |- | ||
− | | 05 | + | | 05:00 |
− | |The output is displayed as "The string is Spoken-Tutorial". | + | |The output is displayed as ''' "The string is Spoken-Tutorial".''' |
|- | |- | ||
− | | 05 | + | | 05:06 |
|Now we will see some common errors which we can come across . | |Now we will see some common errors which we can come across . | ||
|- | |- | ||
− | | 05 | + | | 05:09 |
− | |Come back to our program | + | |Come back to our program. |
− | + | ||
|- | |- | ||
− | | 05 | + | | 05:11 |
− | |Suppose here we type the spelling of string as sting | + | |Suppose, here we type the spelling of string as '''sting'''. |
− | + | ||
|- | |- | ||
− | | 05 | + | | 05:16 |
− | |Now click on Save. | + | |Now click on '''Save.''' |
− | + | ||
|- | |- | ||
− | | 05 | + | | 05:18 |
− | |Let us execute | + | |Let us execute.Come back to our '''terminal'''. |
|- | |- | ||
− | | 05 | + | | 05:21 |
− | | | + | |Now compile as before. |
|- | |- | ||
− | | 05 | + | | 05:23 |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|We see an fatal error. | |We see an fatal error. | ||
− | |||
|- | |- | ||
− | | 05 | + | | 05:25 |
− | |sting.h: | + | |'''sting.h: No such file or directory ''' |
− | + | ||
|- | |- | ||
− | | 05 | + | | 05:28 |
− | |compilation terminated | + | |'''compilation terminated. ''' |
− | + | ||
− | + | ||
|- | |- | ||
− | | 05 | + | | 05:30 |
|Come back to our program. | |Come back to our program. | ||
|- | |- | ||
− | | 05 | + | | 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 | + | | 05:39 |
|Hence it is giving an error. | |Hence it is giving an error. | ||
− | |||
|- | |- | ||
− | | 05 | + | | 05:41 |
|Let us fix the error. | |Let us fix the error. | ||
− | |||
− | |||
|- | |- | ||
− | | 05 | + | | 05:43 |
− | |Type r here. | + | |Type '''r''' here. |
− | + | ||
|- | |- | ||
− | | 05 | + | | 05:45 |
− | |Now click on Save. | + | |Now click on '''Save. '''Let us execute again. |
|- | |- | ||
− | | 05 | + | | 05:47 |
− | | | + | |Come back to our '''terminal. ''' |
|- | |- | ||
− | | 05 | + | | 05:50 |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|Compile as before, execute as before. | |Compile as before, execute as before. | ||
|- | |- | ||
− | | 05 | + | | 05:54 |
|Yes, it is working! | |Yes, it is working! | ||
− | |||
|- | |- | ||
− | | 05 | + | | 05:56 |
|Now, let us see another common error. | |Now, let us see another common error. | ||
|- | |- | ||
− | | 05 | + | | 05:59 |
|Come back to our program. | |Come back to our program. | ||
− | |||
|- | |- | ||
− | | 06 | + | | 06:02 |
− | |Suppose, here | + | |Suppose, here I will type '''int''' in place of '''char'''. |
− | + | ||
|- | |- | ||
− | | 06 | + | | 06:06 |
− | |Now, click on Save . | + | |Now, click on Save .Let us see what happens. |
|- | |- | ||
− | | 06 | + | | 06:09 |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|Come back to our terminal. | |Come back to our terminal. | ||
− | |||
|- | |- | ||
− | | 06 | + | | 06:11 |
|Let me clear the prompt. | |Let me clear the prompt. | ||
|- | |- | ||
− | | 06 | + | | 06:15 |
|Compile as before. | |Compile as before. | ||
|- | |- | ||
− | | 06 | + | | 06:17 |
|We see an error. | |We see an error. | ||
|- | |- | ||
− | | 06 | + | | 06:19 |
− | |Wide character array initialized from non-wide string | + | |'''Wide character array initialized from non-wide string '''. |
− | + | ||
|- | |- | ||
− | | 06 | + | | 06:24 |
− | | | + | |'''format %s expects argument of type 'char' but argument 2 has type 'int' '''. |
− | + | ||
|- | |- | ||
− | | 06 | + | | 06:32 |
|Come back to our program. | |Come back to our program. | ||
|- | |- | ||
− | | 06 | + | | 06:36 |
− | |This is because we | + | |This is because we used ''' %s''' as the '''format specifier''' for '''string.''' |
|- | |- | ||
− | | 06 | + | | 06:42 |
− | |And we are initializing it with an integer data type | + | |And we are initializing it with an '''integer''' data type. |
− | + | ||
|- | |- | ||
− | | 06 | + | | 06:47 |
|Let us fix the error. | |Let us fix the error. | ||
|- | |- | ||
− | | 06 | + | | 06:49 |
− | |Type char here. | + | |Type '''char''' here. |
− | + | ||
|- | |- | ||
− | | 06 | + | | 06:51 |
− | |Click on Save. | + | |Click on '''Save. ''' |
|- | |- | ||
− | | 06 | + | | 06:53 |
− | |Let us execute. Come back to our terminal. | + | |Let us execute. Come back to our '''terminal.''' |
|- | |- | ||
− | | 06 | + | | 06:56 |
|Compile as before, execute as before. | |Compile as before, execute as before. | ||
− | |||
|- | |- | ||
− | | 07 | + | | 07:00 |
− | |Yes it is working! | + | |Yes, it is working! |
|- | |- | ||
− | | 07 | + | | 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 | + | | 07:08 |
|Come back to our program. | |Come back to our program. | ||
− | |||
|- | |- | ||
− | | 07 | + | | 07:11 |
− | |Let me open our file string.c | + | |Let me open our file '''string.c'''. |
|- | |- | ||
− | | 07 | + | | 07:15 |
|We will edit the code here. | |We will edit the code here. | ||
− | |||
|- | |- | ||
− | | 07 | + | | 07:18 |
− | |First, press shift, ctrl and S | + | |First, press '''shift, ctrl''' and '''S''' keys simultaneously on your keyboard. |
− | + | ||
|- | |- | ||
− | | 07 | + | | 07:25 |
− | |Now save the file with an extension .cpp | + | |Now save the file with an extension '''.cpp''' (dot cpp). |
|- | |- | ||
− | | 07 | + | | 07:29 |
− | |and click on Save. | + | |and click on '''Save. ''' |
− | + | ||
|- | |- | ||
− | | 07 | + | | 07:33 |
− | |Now we will change the header file as iostream. | + | |Now we will change the header file as ''' iostream. ''' |
|- | |- | ||
− | | 07 | + | | 07:38 |
− | |Include the using statement. | + | |Include the '''using statement. ''' |
|- | |- | ||
− | | 07 | + | | 07:43 |
− | |Now click on Save. | + | |Now click on '''Save.''' |
− | + | ||
|- | |- | ||
− | | 07 | + | | 07:47 |
|Now we will delete this declaration. | |Now we will delete this declaration. | ||
− | |||
|- | |- | ||
− | | 07 | + | | 07:50 |
− | |And will declare a string variable. | + | |And will declare a '''string variable.''' |
|- | |- | ||
− | | 07 | + | | 07:53 |
− | |Type string space strname and a semicolon | + | |Type '''string space strname''' and a '''semicolon'''. |
|- | |- | ||
− | | 07 | + | | 07:59 |
− | |Click on Save. | + | |Click on '''Save.''' |
− | + | ||
|- | |- | ||
− | | 08 | + | | 08:02 |
− | | Replace the printf statement with the cout statement. | + | | Replace the '''printf''' statement with the '''cout''' statement. |
|- | |- | ||
− | | 08 | + | | 08:07 |
|Delete the closing bracket here. | |Delete the closing bracket here. | ||
− | |||
|- | |- | ||
− | | 08 | + | | 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) '''. |
|- | |- | ||
− | | 08 | + | | 08:24 |
− | |At the end, type a semicolon. | + | |At the end, type a '''semicolon. ''' |
− | + | ||
− | + | ||
|- | |- | ||
− | | 08 | + | | 08:28 |
− | |Now again, replace the printf statement with the cout statement. | + | |Now again, replace the '''printf''' statement with the '''cout''' statement. |
|- | |- | ||
− | | 08 | + | | 08:36 |
− | |Delete the format specifier and \n | + | |Delete the '''format specifier''' and '''\n '''. |
− | + | ||
|- | |- | ||
− | | 08 | + | | 08:40 |
− | |Now delete the comma | + | |Now delete the '''comma'''. |
− | + | ||
|- | |- | ||
− | | 08 | + | | 08:42 |
|Type two opening angle brackets, delete the bracket here. | |Type two opening angle brackets, delete the bracket here. | ||
− | |||
|- | |- | ||
− | | 08 | + | | 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 | + | | 08:54 |
− | |And click on Save | + | |And click on '''Save'''. |
|- | |- | ||
− | | 08 | + | | 08:58 |
− | |Here we have declared a string variable 'strname' | + | |Here we have declared a '''string variable 'strname' '''. |
|- | |- | ||
− | | 09 | + | | 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. ''' |
− | + | ||
|- | |- | ||
− | | 09 | + | | 09:13 |
− | |Here we use getline to extract the characters from the input sequence. | + | |Here we use '''getline''' to extract the characters from the input sequence. |
− | + | ||
|- | |- | ||
− | | 09 | + | | 09:18 |
− | |It stores them as a string. | + | |It stores them as a '''string'''. |
− | + | ||
|- | |- | ||
− | | 09 | + | | 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 | + | | 09:27 |
|Let me clear the prompt. | |Let me clear the prompt. | ||
+ | |||
|- | |- | ||
− | | 09 | + | | 09:30 |
|To compile, type | |To compile, type | ||
|- | |- | ||
− | | 09 | + | | 09:32 |
− | |g++ space string.cpp space -o space str3 | + | |'''g++ space string.cpp space -o space str3 ''' |
− | + | ||
|- | |- | ||
− | | 09 | + | | 09:39 |
− | |and press Enter. | + | |and press '''Enter. ''' |
− | + | ||
|- | |- | ||
− | | 09 | + | | 09:41 |
− | |To execute, type ./str3 | + | |To execute, type '''./str3 '''(dot slash str3). |
− | + | ||
|- | |- | ||
− | | 09 | + | | 09:46 |
− | |Press Enter. | + | |Press '''Enter. '''It is displayed as '''Enter the string'''. |
− | + | ||
|- | |- | ||
− | | 09 | + | | 09:50 |
− | | | + | |I will enter as '''Talk To A Teacher '''. |
|- | |- | ||
− | | 09 | + | | 09:55 |
− | | | + | |Now press '''Enter'''. |
− | + | ||
|- | |- | ||
− | | 09 | + | | 09:57 |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|The output is displayed as | |The output is displayed as | ||
|- | |- | ||
− | | 09 | + | | 09:59 |
− | |"The string is Talk To A Teacher" | + | | "The string is Talk To A Teacher" |
− | + | ||
|- | |- | ||
− | | 10 | + | | 10:03 |
|We can see that the output is similar to our C code. | |We can see that the output is similar to our C code. | ||
|- | |- | ||
− | | 10 | + | | 10:07 |
− | |Now come back to our slides. | + | |Now come back to our slides. Let us summarize. |
− | + | ||
|- | |- | ||
− | | 10 | + | | 10:11 |
− | + | |In this tutorial we learnt: '''Strings ''' | |
− | + | ||
− | + | ||
− | + | ||
− | |In this tutorial we learnt | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | |||
|- | |- | ||
− | | 10 | + | | 10:14 |
− | |Declaration of a string | + | |Declaration of a '''string ''' |
− | + | ||
|- | |- | ||
− | | 10 | + | | 10:16 |
− | |eg: char strname[30] | + | |'''eg: char strname[30] ''' |
|- | |- | ||
− | |10 | + | |10:20 |
− | |Initialization of a string | + | |Initialization of a '''string '''eg: '''char strname[30] = “Talk To A Teacher”''' |
− | + | ||
|- | |- | ||
− | | 10 | + | | 10:26 |
− | | | + | |As an assignment, |
|- | |- | ||
− | | 10 | + | | 10:28 |
− | | | + | |write a program to print a '''string''' using the 2nd syntax. |
|- | |- | ||
− | | 10 | + | | 10:34 |
− | | | + | |Watch the video available at the link shown below. |
|- | |- | ||
− | | 10 | + | | 10:37 |
− | | | + | |It summarizes the Spoken Tutorial project. |
|- | |- | ||
− | | 10 | + | | 10:40 |
− | | | + | |If you do not have good bandwidth, you can download and watch it. |
|- | |- | ||
− | | 10 | + | |10:44 |
− | | | + | |The Spoken Tutorial Project Team: |
|- | |- | ||
− | |10 | + | | 10:46 |
− | | | + | |Conducts workshops using spoken tutorials. |
|- | |- | ||
− | | 10 | + | | 10:49 |
− | | | + | |Gives certificates to those who pass an online test. |
|- | |- | ||
− | | 10 | + | | 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 | + | |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 | + | | 11:12 |
− | |More information on this | + | |More information on this mission is available at the link shown below. |
|- | |- | ||
− | | 11 | + | | 11:16 |
− | |This is Ashwini Patil from IIT Bombay signing off. | + | |This is Ashwini Patil from IIT Bombay, signing off. |
|- | |- | ||
− | | 11 | + | | 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. |