C-and-C++/C3/Strings/Gujarati

From Script | Spoken-Tutorial
Revision as of 00:54, 2 April 2014 by Jyotisolanki (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
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


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.


04.31 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
05.19 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.
05.46 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 .
06.07 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 key simultaneously on your keyboard.


07.25 Now save the file with an extension .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


09.46 Press Enter.


09.47 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.


10.10 Let us summarize
10.11 In this tutorial we learnt


10.13 Strings


10.14 Declaration of a string


10.16 eg: char strname[30]
10.20 Initialization of a string


10.21 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.

Contributors and Content Editors

Jyotisolanki, Pratik kamble