Difference between revisions of "C-and-C++/C3/String-Library-Functions/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
Line 17: Line 17:
  
  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Welcome to the spoken-tutorial on String
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Welcome to the spoken-tutorial on '''String Library Functions in C'''
 
+
Library Functions in C
+
  
 
|-
 
|-
Line 60: Line 58:
 
| 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 see some of the string library functions
 
| 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 see some of the string library functions
  
Here we have the strncpy function  
+
Here we have the '''strncpy''' function  
  
The syntax for this is strncpy(char str1, char str2, int n )
+
The syntax for this is  
 +
'''strncpy(char str1, char str2, (and) int n )'''
  
It copies first n characters of string str2 into string str1.
+
It copies first n characters of string '''str2''' into string '''str1.'''
  
 
eg.  
 
eg.  
  
char strncpy(char hello, char world, 2);
+
'''char strncpy(char hello, char world, 2);'''
  
 
The output will be  
 
The output will be  
  
Wollo
+
'''Wollo'''
  
Here we have Wo fromt the string 2 and rest of the characters from string 1
+
Here we have '''Wo''' from the string 2 and rest of the characters from string 1
  
 
|-
 
|-
Line 81: Line 80:
  
  
| 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;"| Now we will see strcmpy function int strncmp, the syntax for this is (char str1, char str2, int n)
+
| 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;"| Now we will see '''strcmpy''' function , the syntax for this is  
 +
 
 +
'''(char str1, char str2, int n)'''
  
 
It will compare first n characters of string 2 with string 1
 
It will compare first n characters of string 2 with string 1
Line 87: Line 88:
 
eg.  
 
eg.  
  
int strncmp(char ice, char icecream, int 2);
+
'''int strmcmp(char ice, char icecream, (and) int 2);'''
  
 
The output will be 0
 
The output will be 0
Line 109: Line 110:
 
|-
 
|-
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| //strlen()
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| //strlen()
| 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;"| Note that our filename is strlen.c.
+
| 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;"| Note that our filename is '''strlen.c.'''
  
 
In this we will find the length of the string  
 
In this we will find the length of the string  
  
These are the header files as '''stdio.h and string.h '''Make only one file which contain all the code / function.
+
These are the header files as '''stdio.h and string.h '''
  
 
This is our main function
 
This is our main function
Line 126: Line 127:
  
 
Then we have an integer variable '''len1 '''
 
Then we have an integer variable '''len1 '''
 
 
  
  
Line 134: Line 133:
  
 
int len1 = strlen(arr);
 
int len1 = strlen(arr);
| 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;"| here''' '''we will find the lenght of the string using strlen funtion
+
| 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;"| Here''' '''we will find the length of the string using strlen funtion
  
 
the result will be stored in len1
 
the result will be stored in len1
Line 145: Line 144:
  
 
And this is our return statement  
 
And this is our return statement  
 
 
 
  
 
|-
 
|-
Line 170: Line 166:
 
"gcc" space "strlen.c" space “-o” space “str1”.
 
"gcc" space "strlen.c" space “-o” space “str1”.
  
Press Enter
+
'''Press Enter'''
  
 
|-
 
|-
Line 180: Line 176:
 
'''./str1'''  
 
'''./str1'''  
  
Press Enter  
+
'''Press Enter '''
  
 
|-
 
|-
Line 200: Line 196:
 
|-
 
|-
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| //strcpy
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| //strcpy
| 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;"| Note that our filename is strcpy.c
+
| 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;"| Note that our filename is '''strcpy.c'''
  
 
|-
 
|-
Line 209: Line 205:
  
 
Here we have Ice in the source string, it will be copied to the target string  
 
Here we have Ice in the source string, it will be copied to the target string  
 +
This is our '''strcpy''' function.
  
 
|-
 
|-
Line 260: Line 257:
 
| 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;"| Now we will see the string compare function  
 
| 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;"| Now we will see the string compare function  
  
Note that our filename is strcmp.c
+
Note that our filename is '''strcmp.c'''
  
 
In this we will comapre two strings  
 
In this we will comapre two strings  
  
Here we have character variables as str1 and str2  
+
Here we have character variables as '''str1''' and '''str2 '''
  
 
|-
 
|-
Line 272: Line 269:
  
 
<nowiki>char target[]= "Hello";</nowiki>
 
<nowiki>char target[]= "Hello";</nowiki>
| 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;"| str 1 stores the value as ''''Ice'''' and str2 stores the value as ''''Cream'.'''
+
| 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;"| '''str1''' stores the value as ''''Ice'''' and '''str2''' stores the value as ''''Cream'.'''
  
Here we have interger variables as i and j
+
Here we have interger variables as i and j.
  
In this we will compare the string using the strcmp function
+
In this we will compare the string using the strcmp function.
  
Here we compare str1 ie: 'Ice' with 'Hello'
+
Here we compare str1 ie: 'Ice' with 'Hello'.
  
The result is stored in i
+
The result is stored in i.
  
In this we will compare string 2 ie: 'Cream' with 'Cream'
+
In this we will compare string 2 ie: 'Cream' with 'Cream'.
  
The result is stored in j
+
The result is stored in j.
 
+
Then we print both the results
+
 
+
and this is our return statement
+
  
 +
Then we print both the results.
  
 +
and this is our return statement.
  
  
Line 342: Line 337:
  
 
and strncmp()
 
and strncmp()
 
 
  
  
Line 399: Line 392:
 
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
  
More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro
+
More information on this Mission is available at the link shown below: http://spoken-tutorial.org\NMEICT-Intro
  
 
|-
 
|-

Latest revision as of 12:57, 31 January 2014

Title of script: String Library Functions

Author: Ashwini R. Patil

Keywords: String Library Functions, strcpy, strcat, strlen, Video Tutorial


Visual Cue
Narration


Slide 1


Welcome to the spoken-tutorial on String Library Functions in C
Slide 2

Learning Objectives

In this tutorial, we will learn,

String Library Functions

We will do this with the help of some examples

Slide 3

System Requirements

To record this tutorial, I am using

Ubuntu Operating System version 11.10,

gcc Compiler Version 4.6.1

Slide 4

Introduction to the standard library funtions

Let us start with an introduction to string library functions.

These are the group of functions implementing operations on strings.

Various operations such as copying, concatenation, searching etc are supported.

Slide 5

String Library functions


Let us see some of the string library functions

Here we have the strncpy function

The syntax for this is strncpy(char str1, char str2, (and) int n )

It copies first n characters of string str2 into string str1.

eg.

char strncpy(char hello, char world, 2);

The output will be

Wollo

Here we have Wo from the string 2 and rest of the characters from string 1

Slide 6


Now we will see strcmpy function , the syntax for this is

(char str1, char str2, int n)

It will compare first n characters of string 2 with string 1

eg.

int strmcmp(char ice, char icecream, (and) int 2);

The output will be 0

Examples of some commonly used String functions

Please open the Text Editor

Go to Applications – Accessories – Text Editor

Now we will see how to use the string library functions.

I am going to show you some of the commonly used string functions.

I have already typed the program on the editor,

i will open it

Here we have the string length function

//strlen() Note that our filename is strlen.c.

In this we will find the length of the string

These are the header files as stdio.h and string.h

This is our main function

Highlight

char arr[] = "Icecream";

Here we have a character variable 'arr',

It stores a value 'Ashwini'

Then we have an integer variable len1


Highlight

int len1 = strlen(arr);

Here we will find the length of the string using strlen funtion

the result will be stored in len1

Highlight

printf("string = %s length = %d\n", arr, len1);

Then we print the string and the length of the string.

And this is our return statement

Execution of the program


Ctrl, Alt and T keys simultaneously

Now let us execute the program

Open the terminal window by pressing

Ctrl, Alt and T keys simultaneously on your keyboard

Type

gcc strlen.c -o len

To compile

Type:

"gcc" space "strlen.c" space “-o” space “str1”.

Press Enter

Type

./str1

type:

./str1

Press Enter

Highlight

Output

The output is displayed as

string = Ashwini, Length = 7

You can count here. 1,2,3,4,5,6, and 7

Let us see another string function

Here we have the string copy fuction

//strcpy Note that our filename is strcpy.c
Highlight

strcpy(target, source);

In this we will copy the source string into the target string

Here we have Ice in the source string, it will be copied to the target string This is our strcpy function.

Highlight

printf("source string = %s\n",source);

Here we print the source string and the target string
Execution of the program

Please Switch back to the terminal

Let us execute and see

Come back to our terminal

To compile type

gcc space strcpy.c space -o space str2

Press Enter

Type

./str2

Press Enter

Everywhere you change the file name and tell them to compile as before. Yes, i want an explanation?

* Ignore this, i deleted.

Highlight

Output

The output is displayed as

source string = Ice

target string = Ice

Now let us see another string function
//strcat Now we will see the string compare function

Note that our filename is strcmp.c

In this we will comapre two strings

Here we have character variables as str1 and str2

Highlight

char source[]= "World";

char target[]= "Hello";

str1 stores the value as 'Ice' and str2 stores the value as 'Cream'.

Here we have interger variables as i and j.

In this we will compare the string using the strcmp function.

Here we compare str1 ie: 'Ice' with 'Hello'.

The result is stored in i.

In this we will compare string 2 ie: 'Cream' with 'Cream'.

The result is stored in j.

Then we print both the results.

and this is our return statement.


Execution of the program

Please switch back to the terminal

Let us execute the program.

Come back to our terminal.

To compile type

gcc space strcmp.c space -o space str3

Press Enter

Type

./str3

The outpur is displayed as 1,0

Come back to our program

Here we get 1

and here we get as 0

Let us come back to our slides

Let us summarize,

Summary In this tutorial we learned,

String library functions

strlen()

strcpy()

strcmp()

strncpy()

and strncmp()


Slide 13

Assignment

As an assignemnt,

Write a C Program to concatenate String best and String bus.

Hint: strcat(char str1, char str2);

Also explore the other functions in string library.

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 Number 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 [at] spoken hyphen tutorial dot org

Slide Number 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 the link shown below: http://spoken-tutorial.org\NMEICT-Intro

This is Ashwini Patil from IIT Bombay.

Thank you for joining.

Contributors and Content Editors

Ashwini