Strings 180329060622
Strings 180329060622
Strings 180329060622
"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
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)
Declaring and Initializing String Variables
Allocate an array of a size large enough to hold the string (plus 1 extra value
for the delimiter)
Char string_name[size];
Example: char city[10];
Examples (with initialization):
char str1[6] = “Hello”;
char str2[] = “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) */
• When the compiler assigns a character string to a
character array it automatically supplies a NULL (\o)
character at the end of the string.
We can declare the size much larger than the string size in the initializer.
That is the statement:
char str [10]=“GOOD”; is permitted.
However the following declaration is illegal. Char str2[3]=“Good”; This
will result in compile time error.
We cannot separate the initialization from declaration. i.e. char str3[5];
str3=“Good”;
Similarly, char s1[4]=“abc”;
cahr s2[4];
s2=s1; is not allowed. An array name cannot be used as the left
operand of the assignment operator.
“Why do we need a terminating NULL
character?”
A string is not a datatype in C but it is considered a data
structure stored in an array. The string is a variable length
structure and is stored in a fixed length array. The array
size is not always the size of the string and most often it is
much larger than the string stored in it. Therefore, the last
element of the array need not represent the end of the
string. We need some way to determine the end of the
string data and the NULL character serves as the “end of
string”.
Changing String Variables
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
Reading Strings from terminal
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 character.
Example:
char Name[11];
scanf(“%s”,Name);
‘&’ is not required before the variable name. The unused
locations are filled with garbage.
We can also specify the field width using the form %ws in the
scanf statement for reading a specified number of characters
from the input string. Ex: scanf(“%ws”, name);
Reading Strings from terminal(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
Reading Strings from terminal(cont)
%s and %ws can read strings without white spaces i.e.
they cannot be used for reading a text containing more
than one word. However we can use the following code to
read a line:
char line[80];
scanf(“[^\n]”, line);
printf(“%s”, line);
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 | */
Arithmetic operations on characters
Whenever a character constant or character variable is
used in an expression, it is automatically converted into an
integer value by the system.
Example: x=‘a’; printf(“%d\n”,x); will display 97 on
screen which is ASCII value of ‘a’ in lower case.
Ex-2 x=‘z’-1 will assign value 121 to variable x.
Ex-3 ch>=‘A’ && ch<=‘Z’ would test whether the
character contained in variable ch is an upper case letter.
C has a function “atoi” that converts a string of digits into
their integer values. It is stored in <stdlib.h> Ex:
number=“1988”;
year=atoi(number);
Putting strings together
The process of combining two strings together is called
concatenation.
String3=string1+string2; is not valid.
We can combine the strings using the following program:
include <stdio.h>
int main()
{
char s1[100], s2[100], i, j;
printf("Enter first string: ");
scanf("%s", s1);
printf("Enter second string: ");
scanf("%s", s2); // calculate the length of string s1 // and store it in i
for(i = 0; s1[i] != '\0'; ++i);
for(j = 0; s2[j] != '\0'; ++j, ++i)
{ s1[i] = s2[j]; }
s1[i] = '\0';
printf("After concatenation: %s", s1);
return 0;
}
String Copy:
#include <stdio.h>
int main()
{
char s1[100], s2[100], i;
printf("Enter string s1: ");
scanf("%s",s1);
for(i = 0; s1[i] != '\0'; ++i)
{
s2[i] = s1[i];
}
s2[i] = '\0';
printf("String s2: %s", s2);
return 0;
}
ASSIGNMENT