Chp 9 Puppetting On Strings Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What are strings?

A

Character arrays.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a string constant?

A

Its a one dimensional array terminated by a null (‘\0’).

char name[ ] = { ‘B’, ‘i’, ‘p’, ‘u’, ‘\0’}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Null character :

A
\0
Has two chars but is only 1.
\ indicates special char ahead.
\0 and 0 are diff.
ASCII values 0 and 48 respectively.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What if a string is not terminated by null char?

A

Then its merely a collection of chars, other than null, functions working with string have no choice to know bout ends.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Initialise a String:

A

char name[] = “abcdef”;

C inserts a null character automatically.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Memory map of string ‘abcdef’ :

A

a,b,c,d,e,f,\0

Stored contiguously. 1 char takes 1 byte.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to print out a string when len is unknown?

A

while ( i != ‘\0’ )

we can make use of this statement.
OR
while ( *p != ‘\0’ )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Base address:

All notations for element:

A

We can get it simply by mentioning the name of array.

num[i] = *(num + i) = *(i + num) = i[num]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Format specifier for string:
in printf
and in scanf

and working of scanf with string?

A

%s
now clue here
scanf( ‘%s’ , name );
is valid as name carries base address, or basically address where we want the string.
When enter key is hit, scanf places ‘\0’ in the array.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Precautions

Scanf and String:

A
  1. Length of string should not exceed dimension of character array, as no bound checking, so it might result in overriding imp data.
  2. scanf() cannot receive multi-word string.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to deal with limitation of scanf that it cant accept multi word string:

2 ways:

(2 functions in here )

A

gets().
Receives only one string but it can be multi-word.

Also, puts() : It can display only one string at a time.
And it puts cursor on the next line unlike printf().
OR
scanf( “%[^\n]s”, name );

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Pointers and String:

2 options to store the string sorta..

A

chr str[] = “Hello”;
chr *p = “Hello”;

We cannot assign a string to another. But we can assign a char pointer to another.
str1 = str; /* Error /
p = q; /
q is a char pointer, No error */

String once defined can’t be initialised again. Valid for char pointers.

str = "Bye";   /* Error*/
p = "Bye";      /* works */
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Standard lib functions:

A
Check table.
for now:
strlen(string)
strcmp(string,string)
strcpy(target,string)
strcat()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

strlen()

A

Counts no. of char present in string.

It takes in the base address of string then counts until ‘\0’ encountered and returns length.
It doesn’t count ‘\0’.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

strcpy()

A

strcpy( target, source );

Copies Contents of source in target.
It accepts base address of both strings, string gets copied char by char, no short cut.
Remeber after chars being copied, after loop, target should have ‘\0’ at the end.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

The const keyword:

A

const qualifier ensures that ur program does not inadvertently alter a variable you intend to keep constant.

const char q = ‘hello’; / String fixed, pointer not/
q = ‘M’; / Error /
q = “Bye”; /
Works
/

char const q = “Hello”; / Same as above */

char const t = “Hello”; / Pointer const, string not*/

const can be used for int, float,…etc

17
Q

strcat()

A

strcat(target, source);
Concatenate source at the end of target.
Base addresses passed.

18
Q

strcmp()

A

strcmp( string1, string2 );
returns 0 for equal string, otherwise difference between ASCII values of the char when they first don’t match.
Examples:
strcmp ( “Jerry”, “Jerry” ); /* 0 /
strcmp ( “Jerry”, “Ferry” ); /
4 /
strcmp ( “Jerry”, “Jerry Boy” ); /
-32 */
‘\0’ - ‘ ‘ = 0 - 32 = -32

19
Q

2 D Array of characters:

A

char name[6][10] = { 6 strings of dimension 10 or less. };
[] first subscript give number of names.
second gives length of each item.

20
Q

How to get 2D string, from keyboard?

A

for (i = 0; i<6 ; i++ )

{ scanf( ‘%s’, &name[i][0] ); }

21
Q

Memory map of 2D array of chars:

A

char name[6][10] = { 6 strings of dimension 10 or less. };

each string will be at a diff of 10 bytes in location.
each ends with ‘/0’.
Not necessary every name occupies all the 10 bytes, so unoccupied bytes are wasted , unused.

22
Q

How to take care of wasted bytes in 2D array of chars?

A

By Array of pointers to strings.

23
Q

Array Of Pointers to strings:

Benefits of using it?

A

char *p[6] = {Name of 6 strings};

Since the name contains the base address. No need to mention 6 on LHS in [].

Now we have not saved all the bytes wasted by 2D array method as we need 6*2 bytes for this array.
[2 bytes per int]

Bytes used optimally and manipulation easier.

24
Q

Exchange names using 2 D array and array of pointers to strings:

A

for i in 0 to 9
t = names[2][i]; names[2][i] = names[3][i];
names[3][i] = t;
10characters interchanged here

char *temp;
temp = name[2]; name[2] = name[3];
name[3] = temp;
Here just addresses exchanged.

25
Q

Limitation of Array of Pointers to String:

A
  1. You cannot take values from keyboard in
    char *name[6];
    cuz when declared it contains garbage value, we cant send these garbage values to scanf() as the addresses where it should keep the received strings.
26
Q

The solution to Limitation of Array of Pointers to String:

How is the solutions quality?

A
#include "alloc.h"
char *name[6];
char n[50];
int len, i;
char *p;
for i in 0 to 5
{ scanf( '%s', &amp;n);
  len = strlen(n);
 /* Allocate space for making copy of name
/* void* to char*, typecasting.
  p = malloc(len +1 )
  strcpy(p,n);
  name[i] = p;
}
malloc() : Needs no. of bytes to be allocated and returns base address of the chunk of memory it allocates.
Address returned is always of type void*.
It allocates memory dynamically, unlike arrays where static memory allocation takes place.

This suffers in performance as we need to allocate memory and then do the copying of string for each name received.