strings and pointers

21
Strings and Pointers

Upload: gurpreet-singh-sond

Post on 26-Dec-2014

71 views

Category:

Education


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Strings and pointers

Strings and Pointers

Page 2: Strings and pointers

STRING String is a one dimensional array of

characters. Start with index 0 and end with null

character i.e. \0. A string may include letter,digits and special

characters(+,-,*,/,$).

Page 3: Strings and pointers

Declaration A string can be declared in two ways:- 1. Character array- char color[]=“blue”; 2. Using pointer variable- const char *colorptr[]=“blue”;

Page 4: Strings and pointers

The first declaration creates a five-element array color containing the characters ‘b’ , ‘l’ , ‘u’ , ‘e’ and ‘\0’.

The second declaration creates pointer variable colorptr that points to the character ‘b’ in the string “blue” somewhere in memory.

The declaration char color[]=“blue”; could also be written as:

char color[]={‘b’ , ‘l’ , ‘u’ , ‘e’ , ‘\0’};

Page 5: Strings and pointers

Passing StringsJust as we can pass other kinds of arrays to

functions, we can do so with strings.Below is the definition of a function that prints a

label and a call to that function:

Page 6: Strings and pointers

Since label is a character array, and the function  PrintLabel() expects a character array.However, if we have a pointer to the character

array label, as in: char *labelPtr = label;Then we can also pass the pointer to the

function, as in: PrintLabel(labelPtr);The results are the same. 

Why??

Page 7: Strings and pointers

When we declare an array as the parameter to a function, we really just get a pointer. Plus, arrays are always automatically passed by reference (e.g., a pointer is passed).

So, PrintLabel() could have been written in two ways: void PrintLabel(char the_label[]) { printf("Label: %s\n", the_label); }

OR

void PrintLabel(char *the_label) { printf("Label: %s\n", the_label); }

Page 8: Strings and pointers

Various String Manipulation FunctionsThe library <cstring> has several common

functions for dealing with strings. The following four are the most useful ones that we'll discuss:

strlen(str) :-Returns the number characters in the string, not including the nul character.

Page 9: Strings and pointers

Output of this program is:

Page 10: Strings and pointers

strcmp(str1, str2):- This function takes two strings and compares them. If the strings are equal, it returns 0. If the first is greater than the 2nd, then it returns some value greater than 0. If the first is less than the 2nd, then it returns some value less than 0.

Page 11: Strings and pointers

Output of this program is :-

Page 12: Strings and pointers

The ordering for strings is lexical order based on the ASCII value of characters. Remember that the ASCII value of 'A' and 'a' (i.e., upper/lowercase) are not the same.

An easy way to remember how to use strcmp() to compare 2 strings (let's say a and b) is to use the following mnemonics:

Want…. Use… a==b strcmp(a, b) ==

0

a<b strcmp(a, b) < 0

a>=b strcmp(a, b) >= 0

Page 13: Strings and pointers

strcpy(dest, source) :- Copies the contents of source into dest, as

in:

Page 14: Strings and pointers

Output of this program is:-

Page 15: Strings and pointers

Now, the string str1 contains the following: ------------------------------------------- | s | e | c | o | n | d | \0 | u | e | \0 | ------------------------------------------- and the word "initvalue" has been overwritten. Note that it is the first nul character (\0) that determines the end of string.When using strcpy(), make sure the destination is big enough to hold new string. Aside: An easy way to remember that the destination comes first is because the order is the same as for assignment, e.g: dest = source Also, strcpy() returns the destination string, but that return value is often ignored.

Page 16: Strings and pointers

strcat(dest, source) :- Copies the contents of source onto the end of dest,as in:

Page 17: Strings and pointers

Output of this program is :-

Page 18: Strings and pointers

Strtok(char s1,const char s2) :-

A sequence of calls to strtok breaks string s1 into “tokens” – logical pieces such as words in a line of text- delimited by characters contained in string s2(delimiter). The first call contains s1 as the first argument, and subsequent calls to continue tokenizing the same string contain NULL as the first argument. A pointer to the current token is returned by each call. If there are no more tokens when the function is called NULL is returned.

Page 19: Strings and pointers

Consider following program :-

Page 20: Strings and pointers

Output of this program is :-

Page 21: Strings and pointers

THANK YOU!