Docsity

Strings in C - Computer Science and Engineering - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Strings in C - Computer Science and Engineering - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

STRINGS

IN

C

Topics Covered Are:

  1. Introduction2. Declaration of strings3. Initialization of strings4. Reading strings5. Writing strings6. String handling functions7. Array of strings8. Conclusion

1.1 Features of strings

C has no native string type, instead we

use arrays of char.

A special character, called a “null”,

marks the end.

This may be written as ’\0’.

This is the only character whose ASCIIvalue is zero.

Depending on how arrays of charactersare built, we may need to add the nullby hand , or the compiler may add it forus.

Declaration of strings

The strings can be declared as array ofcharacter.

The general syntax is:

char name_of_string[length];

Char is data type.

Name_of _string is user defined name given to string variable.

[length]: defines the size of the string.

a[0]

a[1]

a[2]

a[3]

a[4]

a[5]

H
A
N
D
A
\

Mem 200

Add. So total 6 bytes are allocated to the string‘a’.

cont. …

CONTD. …

Notice that even though there are only five characters in the world ‘HONDA’ ,six characters are stored in computer.The last character, the character’/0’ isthe NULL character, which indicatesthe end of string.

Therefore , if any array of characters is to be used to store a string , thearray must be large enough to storethe string and its terminating NULLcharacter.

Initialization of strings

if we happen to declare a string like this :

char my_drink[3]= “tea”;

we will get the following syntaxerror: error c2117:’tea’:

array bound

overflow.

instead , we need to at lest declare the array with (the size of string +1) toaccommodate the null terminatecharacter’\0’.

char my_drink[4]=“tea”;

String Input

Use %s field specification in scanf to read string

◦

ignores leading white space

◦

reads characters until next white spaceencountered

◦

C stores null (\0) char after last non-whitespace char

◦

Reads into array (no & before name, array is apointer)

Example:

char Name[11];scanf(“%s”,Name);

Problem: no limit on number of characters read(need one for delimiter), if too many charactersfor array, problems may occur

String Input (cont)

Edit set input %[

ListofChars

]

◦

ListofChars specifies set of characters (called scanset)

◦

Characters read as long as character falls in scan set

◦

Stops when first non scan set character encountered

◦

Note, does not ignored leading white space

◦

Any character may be specified except ]

◦

Putting ^ at the start to negate the set (any characterBUT list is allowed)

Examples:

scanf(“%[-+0123456789]”,Number);scanf(“%[^\n]”,Line); /* read until newline char */

String Output

Use %s field specification in printf:

characters in string printed until \

encountered

char Name[10] = “Rich”;printf(“|%s|”,Name); /* outputs |Rich| */

Can use width value to print string in space:

printf(“|%10s|”,Name); /* outputs |

Rich|

*/

Use - flag to left justify:

printf(“|%-10s|”,Name); /* outputs |Rich

|

*/

docsity.com

Example cont…

Enter your name(last , first):Kaur HarpreetNice to meet you Harpreet Kaur

Readind a string

The drawback of reading a string

using scanf() is that it does not readwhitespaces or whole line

If the string to be read as an input hasembedded whitespace characters, usestandard

gets()

function.

Example of gets()/puts()

#include <stdio.h>void main(void){

char string1[50];char string2[50];printf("Enter a string less than 50characters with spaces: \n ");gets(string1);printf("\nYou have entered: ");puts(string1);

getch();

}

Example cont…

/* Sample output */

Enter a string less than 50 characterswith spaces:hello worldYou have entered: hello world