C-and-C++/C3/Strings/English

From Script | Spoken-Tutorial
Revision as of 22:25, 12 December 2013 by Nancyvarkey (Talk | contribs)

Jump to: navigation, search

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 In this tutorial, we will learn,
  • What is a string.
  • Declaration of the string.
  • Initialization of the string.
  • With the help of an example.

We will aldo see some common errors and their solutions

Slide 3 To record this tutorial, I am using
  • Ubuntu Operating System version 11.04
  • gcc and g++ Compiler version 4.6.1
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];

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


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.


Until now, we discussed about the declaration of a string.

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


Another syntax is char var_name[] = {'S', 't', 'r', 'i', 'n', 'g'};

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


char name[30] = "Talk To A Teacher";


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


gcc strinitialize.c -o str1


./str1

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


Save as

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


#include <string>

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";


In place of

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


About the Spoken Tutorial Project

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


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

More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro

Ashwini Patil from IIT Bombay

Thank You for joining

Contributors and Content Editors

Ashwini, Nancyvarkey