String
String
1
Strings
• 1-d arrays of type char
• By convention, a string in C is terminated by the
end-of-string sentinel ‘\0’ (null character)
• char s[21] - can have variable length string
delimited with \0
• Max length of the string that can be stored is 20 as the
size must include storage needed for the ‘\0’
• String constants : “hello”, “abc”
• “abc” is a character array of size 4
2
Character Arrays and Strings
char C[8] = { 'a', 'b', 'h', 'i', 'j', 'i', 't', '\0' };
C[0] gets the value 'a', C[1] the value 'b', and so on.
The last (7th) location receives the null character ‘\0’
Null-terminated (last character is ‘\0’) character arrays
are also called strings
Strings can be initialized in an alternative way. The
last declaration is equivalent to:
char C[8] = "abhijit";
The trailing null character is missing here. C
automatically puts it at the end if you define it like this
Note also that for individual characters, C uses single
quotes, whereas for strings, it uses double quotes
3
Reading strings: %s format
void main()
{
char name[25];
scanf("%s", name);
printf("Name = %s \n", name);
}
4
An example
void main()
{
#define SIZE 25 Seen on screen
int i, count=0;
char name[SIZE]; Typed as input
scanf("%s", name); Satyanarayana
printf("Name = %s \n", name);
Name = Satyanarayana
for (i=0; name[i]!='\0'; i++)
if (name[i] == 'a') count++; Total a's = 6
printf("Total a's = %d\n", count);
}
Printed by program
Note that character strings read
in %s format end with ‘\0’
5
Differences : array & pointers
char *p = “abcde”; char s[ ] = “abcde”;
The compiler allocates char s[ ] = {‘a’,’b’,’c’,’d’,’e’.’\
space for p, puts the 0’};
string constant “abcde”
in memory somewhere The compiler allocates 6 bytes
else, initializes p with of memory for the array s
the base address of which are initialized with the
the string constant 6 characters
p
S a b c d e \0
a b c d e \0
6
String Constant
• A string constant is treated as a pointer
• Its value is the base address of the string
char *p = “abc”;
p a b c \0
7
Library Functions for String
Handling
You can write your own C code to do different
operations on strings like finding the length of a
string, copying one string to another, appending
one string to the end of another etc.
C library provides standard functions for these
that you can call, so no need to write your own
code
To use them, you must do
#include <string.h>
At the beginning of your program (after #include
<stdio.h>)
8
String functions we will see
strlen : finds the length of a string
strcat : concatenates one string at the end
of another
strcmp : compares two strings
lexicographically
strcpy : copies one string to another
9
strlen() You cannot change contents
of s in the function
10
strcat() You cannot change contents
of s2 in the function
char *strcat (char *s1,
const char *s2); char *strcat(char *s1, const char
Takes 2 strings as *s2)
arguments, {
char *p = s1;
concatenates them,
while (*p != ‘\0’) /* go to end */
and puts the result in ++p;
s1. Returns s1. while(*s2 != ‘\0’)
Programmer must *p++ = *s2++; /* copy */
ensure that s1 points *p = ‘\0’;
return s1;
to enough space to
}
hold the result.
11
strcmp()
int strcmp (const char
*s1, const char *s2);
Two strings are passed
as arguments. An
integer is returned
that is less than,
equal to, or greater
than 0, depending
on whether s1 is
lexicographically
less than, equal to,
or greater than s2. 12
strcmp()
int strcmp (const char
*s1, const char *s2); int strcmp(char *s1, const char *s2)
{
Two strings are passed for (;*s1!=‘\0’&&*s2!=‘\0’; s1++,s2++)
as arguments. An {
integer is returned if (*s1>*s2) return 1;
if (*s2>*s1) return -1;
that is less than,
}
equal to, or greater if (*s1 != ‘\0’) return 1;
than 0, depending if (*s2 != ‘\0’) return -1;
on whether s1 is return 0;
lexicographically }
less than, equal to,
or greater than s2. 13
strcpy()
char *strcpy (char *s1, char *s2);
The characters is the string s2 are copied into s1
until \0 is moved. Whatever exists in s1 is
overwritten. It is assumed that s1 has enough space
to hold the result. The pointer s1 is returned.
14
strcpy()
char *strcpy (char *s1, const char *s2);
The characters is the string s2 are copied into s1 until
‘\0’ is moved. Whatever exists in s1 is overwritten. It
is assumed that s1 has enough space to hold the
result. The pointer s1 is returned.