String Using Arrays
String Using Arrays
"A String" A S t r i n g \0
String Literals
• String literal values are represented by sequences
of characters between double quotes (“)
• Examples
– “” - empty string
– “hello”
• “a” versus „a‟
– „a‟ is a single character value (stored in 1 byte) as the
ASCII value for a
– “a” is an array with two characters, the first is a, the
second is the character value \0
Referring to String Literals
• String literal is an array, can refer to a single
character from the literal as a character
• Example:
printf(“%c”,”hello”[1]);
outputs the character „e‟
• During compilation, C creates space for each
string literal (# of characters in the literal + 1)
– referring to the literal refers to that space (as if it is an
array)
Duplicate String Literals
• Each string literal in a C program is stored at a
different location
• So even if the string literals contain the same
string, they are not equal (in the == sense)
• Example:
– char string1[6] = “hello”;
– char string2[6] = “hello”;
– but string1 does not equal string2 (they are stored at
different locations)
String Variables
• Allocate an array of a size large enough to hold
the string (plus 1 extra value for the delimiter)
• Examples (with initialization):
char str1[6] = “Hello”;
char str2[] = “Hello”;
char *str3 = “Hello”;
char str4[6] = {„H‟,‟e‟,‟l‟,‟l‟,‟o‟,‟\0‟};
• Note, each variable is considered a constant in that
the space it is connected to cannot be changed
str1 = str2; /* not allowable, but we can copy the contents
of str2 to str1 (more later) */
Changing String Variables
• Cannot change space string variables connected to,
but can use pointer variables that can be changed
• Example:
char *str1 = “hello”; /* str1 unchangeable */
char *str2 = “goodbye”; /* str2 unchangeable */
char *str3; /* Not tied to space */
str3 = str1; /* str3 points to same space s1 connected to */
str3 = str2;
Changing String Variables (cont)
• Can change parts of a string variable
char str1[6] = “hello”;
str1[0] = „y‟;
/* str1 is now “yello” */
str1[4] = „\0‟;
/* str1 is now “yell” */
• Important to retain delimiter (replacing str1[5] in
the original string with something other than „\0‟
makes a string that does not end)
• Have to stay within limits of array
String Input
• Use %s field specification in scanf to read string
– ignores leading white space
– reads characters until next white space encountered
– C stores null (\0) char after last non-white space char
– Reads into array (no & before name, array is a pointer)
• Example:
char Name[11];
scanf(“%s”,Name);
• Problem: no limit on number of characters read
(need one for delimiter), if too many characters for
array, problems may occur
String Input (cont)
• Can use the width value in the field specification
to limit the number of characters read:
char Name[11];
scanf(“%10s”,Name);
• Remember, you need one space for the \0
– width should be one less than size of array
• Strings shorter than the field specification are read
normally, but C always stops after reading 10
characters
String Input (cont)
• Edit set input %[ListofChars]
– ListofChars specifies set of characters (called scan set)
– 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 character
BUT 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 \0 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 | */
Input/Output Example
#include <stdio.h>
void main() {
char LastName[11];
char FirstName[11];
void main() {
char *days[7];
char TheDay[10];
int day;
days[0] = "Sunday";
days[1] = "Monday";
days[2] = "Tuesday";
days[3] = "Wednesday";
days[4] = "Thursday";
days[5] = "Friday";
days[6] = "Saturday";
Array of Strings Example
printf("Please enter a day: ");
scanf("%s",TheDay);
day = 0;
while ((day < 7) && (!samestring(TheDay,days[day])))
day++;
if (day < 7)
printf("%s is day %d.\n",TheDay,day);
else
printf("No day %s!\n",TheDay);
}
Array of Strings Example
int samestring(char *s1, char *s2) {
int i;
fclose(instream);
}
fclose(outstream);
}
Reading from a String
The sscanf function allows us to read from a string
argument using scanf rules
First argument of sscanf is string to read from,
remaining arguments are as in scanf
Example:
char buffer[100] = “A10 50.0”;
sscanf(buffer,”%c%d%f”,&ch,&inum,&fnum);
/* puts „A‟ in ch, 10 in inum and 50.0 in fnum */