





































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This lecture is part of lecture series on Computer Science and Engineering course. This course is designed for Engineering students. Keyword for this lecture are: Strings in C, Introduction, Declaration of Strings, Initializaton of Strings, Reading Strings, Writing Strings, Handling Functions, Array of Strings
Typology: Slides
1 / 45
This page cannot be seen from the preview
Don't miss anything!






































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.
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]
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.
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”;
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
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 */
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
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.
#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();
}
/* Sample output */
Enter a string less than 50 characterswith spaces:hello worldYou have entered: hello world