C-and-C++/C3/String-Library-Functions/English

From Script | Spoken-Tutorial
Revision as of 13:42, 3 May 2013 by Ashwini (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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 on ubuntu

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 = original 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];

Please open the Terminal


Now we will see how to declare a string with an

example

I have already typed the program

So I will just open it

Note that I have saved the file with the name string.c

Let me explain the code now

Highlight

#include<string.h>

Here, string.h is a header file

It 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];

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

Here we print the string
Save the program Now click on Save to save the program
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

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

Enter the string is displayed here
Type

Talk To A Teacher >> press Enter


I will type

Talk To A Teacher

and 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

This is done by specifying the value

To initialize a string the syntax is

char var_name[size] = “string”;

(or)

char var_name[] = {'S', 't', 'r'};

Please switch to the Text editor


Let me show you how to use the first syntax with an example

Please switch back to the Text Editor

Let me use the same example

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