Difference between revisions of "C-and-C++/C3/Strings/English"
(Created page with ''''Title of script''': Strings '''Author: '''Ashwini R Patil '''Keywords: Strings in C, getline, Video Tutorial''' {| style="border-spacing:0;" | style="border-top:0.002cm s…') |
Nancyvarkey (Talk | contribs) |
||
Line 9: | Line 9: | ||
| style="border-top:0.002cm solid #000000;border-bottom:0.002cm solid #000000;border-left:0.002cm solid #000000;border-right:none;padding:0.097cm;"| <center>'''Visual Cue'''</center> | | style="border-top:0.002cm solid #000000;border-bottom:0.002cm solid #000000;border-left:0.002cm solid #000000;border-right:none;padding:0.097cm;"| <center>'''Visual Cue'''</center> | ||
| style="border:0.002cm solid #000000;padding:0.097cm;"| <center>'''Narration'''</center> | | style="border:0.002cm solid #000000;padding:0.097cm;"| <center>'''Narration'''</center> | ||
− | |||
|- | |- | ||
Line 17: | Line 16: | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 2 | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 2 | ||
− | |||
− | |||
− | |||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial, we will learn, | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial, we will learn, | ||
− | What is a string. | + | *What is a '''string'''. |
− | + | *Declaration of the '''string'''. | |
− | Declaration of the string. | + | *Initialization of the '''string'''. |
− | + | *With the help of an example. | |
− | Initialization of the string. | + | |
− | + | ||
− | With the help of an example. | + | |
We will aldo see some common errors and their solutions | We will aldo see some common errors and their solutions | ||
Line 34: | Line 27: | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 3 | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 3 | ||
− | |||
− | |||
− | |||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To record this tutorial, I am using | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To record this tutorial, I am using | ||
− | '''Ubuntu Operating System''' version 11.04 | + | *'''Ubuntu Operating System''' version 11.04 |
− | + | *'''gcc''' and '''g++''' '''Compiler''' version 4.6.1 | |
− | '''gcc''' and '''g++''' '''Compiler''' version 4.6.1 | + | |
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 4 | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 4 | ||
− | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us start with the | + | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us start with the introduction to the '''Strings''' |
'''String''' is a sequence of characters that is treated as a single data item | '''String''' is a sequence of characters that is treated as a single data item | ||
− | + | Size of '''string''' = length of '''string''' + 1 | |
|- | |- | ||
Line 56: | Line 45: | ||
Declaration of the String | Declaration of the String | ||
− | Syntax: | + | Syntax: <nowiki>char val[10];</nowiki> |
− | + | ||
− | <nowiki>char val[10];</nowiki> | + | |
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let me tell you how to declare a '''string''' | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let me tell you how to declare a '''string''' | ||
Line 64: | Line 51: | ||
'''<nowiki>char name_of_string[size]; </nowiki>''' | '''<nowiki>char name_of_string[size]; </nowiki>''' | ||
+ | *'''char''' is the data-type | ||
+ | *'''name_of_string''' is the '''string''' name | ||
+ | * and we can give the size here | ||
+ | |||
+ | Eg- Here we have declared a '''character string "names"''' with size 10. Now we will see an example. | ||
+ | |||
|- | |- | ||
Line 70: | Line 63: | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now we will see | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now we will see an |
example | example | ||
− | I have already typed the program | + | I have already typed the program. |
− | So I will | + | So I will open it. |
− | Note that | + | Note that our filename is '''string.c''' |
− | Let me explain the code now | + | |- |
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | ||
+ | |||
+ | '''<nowiki>#include<string.h></nowiki>''' | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|In this program, we will take a '''string''' as input from the user and print it. | ||
+ | |||
+ | Let me explain the code now. | ||
|- | |- | ||
Line 86: | Line 85: | ||
'''<nowiki>#include<string.h></nowiki>''' | '''<nowiki>#include<string.h></nowiki>''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| These are our header files. |
− | + | 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. |
|- | |- | ||
Line 96: | Line 95: | ||
'''<nowiki>char strname[30]</nowiki>'''<nowiki>; </nowiki> | '''<nowiki>char strname[30]</nowiki>'''<nowiki>; </nowiki> | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we are declaring the '''string | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is our '''main function'''. Here we are declaring the '''string strname'''' with size ''''30'''' |
|- | |- | ||
Line 102: | Line 101: | ||
'''printf("Enter the string\n"); ''' | '''printf("Enter the string\n"); ''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we are accepting a '''string''' from the user | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we are accepting a '''string''' from the user. |
|- | |- | ||
Line 113: | Line 112: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To read a '''string''', we can use '''scanf()''' function with format specifier '''%s''' | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To read a '''string''', we can use '''scanf()''' function with format specifier '''%s''' | ||
− | Here we are using the '''caret''' sign and''' \n''' to include the spaces with the '''string''' | + | Here we are using the '''caret''' sign and''' \n''' to include the spaces with the '''string'''. |
|- | |- | ||
Line 119: | Line 118: | ||
'''printf("The string is %s\n", strname);''' | '''printf("The string is %s\n", strname);''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we print the '''string'''. |
+ | And this is our '''return statement'''. | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Save the program | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Save the program | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now click on '''Save''' | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now click on '''Save'''. |
|- | |- | ||
Line 134: | Line 134: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us execute the program | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us execute the program | ||
− | Please open the terminal window by pressing '''Ctrl, Alt and T '''keys simultaneously | + | Please open the '''terminal window''' by pressing '''Ctrl, Alt and T '''keys simultaneously on your keyboard. |
|- | |- | ||
Line 142: | Line 142: | ||
>> press '''Enter''' | >> press '''Enter''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To compile | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To compile, type |
− | + | ||
− | + | ||
'''gcc strdeclare.c str''' | '''gcc strdeclare.c str''' | ||
− | And press '''Enter''' | + | And press '''Enter'''. |
|- | |- | ||
Line 156: | Line 154: | ||
>> press '''Enter''' | >> press '''Enter''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To execute, | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To execute, type |
− | + | ||
− | type | + | |
'''./str ''' | '''./str ''' | ||
− | and press '''Enter''' | + | and press '''Enter'''. |
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Enter the string ''' | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here it is displayed as '''Enter the string '''. |
|- | |- | ||
Line 179: | Line 175: | ||
'''Talk To A Teacher''' | '''Talk To A Teacher''' | ||
− | + | Now, press '''Enter'''. | |
|- | |- | ||
Line 191: | Line 187: | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 6 | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 6 | ||
− | |||
Initialization of the string | Initialization of the string | ||
− | |||
Syntax: | Syntax: | ||
− | |||
<nowiki>char strinitialize[20] = “star”;</nowiki> | <nowiki>char strinitialize[20] = “star”;</nowiki> | ||
− | |||
(or) | (or) | ||
− | |||
<nowiki>char strinitialize[] = {'S', 't', 'r'};</nowiki> | <nowiki>char strinitialize[] = {'S', 't', 'r'};</nowiki> | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us switch back to the slides | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us switch back to the 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 | |
− | + | ||
− | + | ||
'''<nowiki>char var_name[size] = “string”;</nowiki>''' | '''<nowiki>char var_name[size] = “string”;</nowiki>''' | ||
− | + | Eg- Here we have declared a '''character string "names"''' with size 10 and the string is '''"Priya"'''. | |
− | '''<nowiki>char var_name[] = </nowiki>{'S', 't', 'r'};''' | + | |
+ | Another syntax is | ||
+ | '''<nowiki>char var_name[] = </nowiki>{'S', 't', 'r', 'i', 'n', 'g'};''' | ||
+ | |||
+ | Eg-char names 10 is equal to Priya within single quotes. | ||
|- | |- | ||
Line 228: | Line 221: | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let me show you how to use the first syntax with an example | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let me show you how to use the first syntax with an example. |
− | + | Switch back to the Editor. | |
+ | |||
+ | We will use the same example. | ||
+ | |||
+ | |- | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | ||
− | + | <span style= "color:red">I CANNOT EDIT THIS SCRIPT ANYMORE. ASHWINI, PLEASE UPDATE THE SCRIPT TO MATCH THE NARRATION OF THE VIDEO ON THE UPLOAD INTERFACE.</span> | |
|- | |- |
Revision as of 22:25, 12 December 2013
Title of script: Strings
Author: Ashwini R Patil
Keywords: Strings in C, getline, Video Tutorial
|
|
Slide 1 | Welcome to the spoken-tutorial on Strings in C and C++ |
Slide 2 | In this tutorial, we will learn,
We will aldo see some common errors and their solutions |
Slide 3 | To record this tutorial, I am using
|
Slide 4 | Let us start with the introduction to the Strings
String is a sequence of characters that is treated as a single data item Size of string = length of string + 1 |
Slide 5
Declaration of the String Syntax: char val[10]; |
Let me tell you how to declare a string
The syntax for this is char name_of_string[size];
Eg- Here we have declared a character string "names" with size 10. Now we will see an example.
|
Please open the Terminal
|
Now we will see an
example I have already typed the program. So I will open it. Note that our filename is string.c |
#include<string.h> |
In this program, we will take a string as 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
Here 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 strdeclare.c str And press Enter. |
Type
./str >> press Enter |
To execute, type
./str and 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 |
And the output is displayed as
The string is Talk To A Teacher |
Slide 6
Initialization of the string Syntax: char strinitialize[20] = “star”; (or) char strinitialize[] = {'S', 't', 'r'}; |
Let us switch back to the 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 10 and the string is "Priya".
Eg-char names 10 is equal to Priya within single quotes. |
Please switch to the Text editor
|
Let me show you how to use the first syntax with an example.
Switch back to the Editor. We will use the same example. |
I CANNOT EDIT THIS SCRIPT ANYMORE. ASHWINI, PLEASE UPDATE THE SCRIPT TO MATCH THE NARRATION OF THE VIDEO ON THE UPLOAD INTERFACE. | |
Click on
File >> Save as option Type strinitialize.c |
First click on File menu
Click on Save as option Let us save the file with the name strinitialize.c |
Click on save | Now click on Save |
Type
|
Let us change a few things here
Let us replace the 5th line with char strname [30] = “Talk To A Teacher”; Here we have initialized a character array strname with size '30' |
Delete
printf("Enter the string\n"); scanf("%[^\n]s",strname); |
Please remove the next two lines
as we only want to print the string We are not taking the string as input from the user |
Save the program | Now let us save the program and execute it |
On the terminal
Type gcc strinitialize.c -o str1
|
Come back to the terminal
Let us compile as before Type gcc strinitialize.c -o str1 |
To execute
Type ./str1 |
To execute
Type ./str1 |
Highlight
Output |
Here the output is displayed as
The string is Talk To A Teacher |
Error1
Type sting.h |
Let us see some common errors which we can come across
Suppose here we type the spelling of string as sting |
Click on Save | I will retain rest of the code as it is
Click on Save |
On the terminal | To execute the program, we will go on the terminal |
Compile and execute as before
|
Let us compile and execute as before
We see that there is a fatal error The compiler is not able to find the header file with the name <sting.h> Hence the compilation is terminated |
On the editor, correct the spelling | Let us fix this error
Come back to the program Type r here |
On the terminal | Come back on the terminal
Let us compile and execute as before Yes it is working! |
Error2
Type int |
Let us see another common error
Suppose I type int here in place of char Let us see what happens Come back on the terminal |
On the terminal | Let us compile and execute as before
We see there is an error and a warning The format specifier for string is %s And we are declaring it with an integer data type int |
On the editor | Let us fix the error
Come back to our program Let us type char here |
Click on save | Click on Save |
On the terminal | Come back to our terminal
Let us compile and execute as before |
Yes it is working! | |
On the editor | NOW LET US SEE HOW TO EXECUTE THE SAME PROGRAM IN C++
Come back to our program |
Click on File
Type .cpp |
First let us save the file with an extention .cpp
Click on File menu Click on Save as option Now save the file with an extension .cpp |
Let me change a few things here | |
Type
iostream |
First we will change the header file as
iostream |
Type
|
We will include another header file here
Type #include <string> |
Type
using namespace std; |
We will include the using statement here |
Type
string strname; |
Let us define a string variable strname
Since we don't use the format specifier in C++ , the compiler should know that strname is a string variable |
Type
cout << |
Now replace the printf statement with cout statement
|
Delete scanf
Type getline(cin, strname);
|
Now delete the scanf statement here and
Type getline(cin, strname); |
We use getline to extract the characters from the input sequence
It stores them as a string | |
Type
cout <<"The string is " <<strname <<"\n";
printf("The string is %s\n",strname); |
Now again replace the printf statement with cout statement
Delete the format specifier and \n here Type two opening angle brackets Then again type two opening angle brackets here and type within the double quotes \n Delete the closing bracket here |
Click on save | Now click on Save |
On the terminal | Come back on the terminal |
Type
g++ string.cpp -o str1 |
To compile type
g++ string.cpp -o str1 |
Type
./str1 |
To execute type
./str1 |
Highlight >> type Talk To A Teacher | Enter the string is displayed here
I will type Talk To A Teacher |
The output is displayed as
The string is Talk To A Teacher | |
Slide 7
Assignment |
As an assignment
Write a program to print a string using the 2nd syntax |
Slide 8
|
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 9
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 10
|
Spoken Tutorial Project is a part of the Talk to a Teacher project
It is supported by the National Mission on Education through ICT, MHRD, Government of India More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro |
Ashwini Patil from IIT Bombay
Thank You for joining |