Strings
Strings
Strings
Outline
• Introduction to string
– Declaration
– Initialization
• Reading and writing strings
– functions of the standard input/output library
(stdio.h)
• Processing of strings.
– String Manipulation Functions from the String
Handling Library
• Comparing strings
– Determining the length of string
Introduction
• Use of C standard library functions for strings
and characters:
– Easy string and character processing
– Programs can process characters, strings, lines of
text, and blocks of memory
• These techniques are used to make
– Word processors
– Page layout software
– Typesetting programs
Fundamentals of characters
• Characters
– Building blocks of programs
• Every program is a sequence of meaningfully grouped
characters
– Character constant
• An int value represented as a character in single quotes
• 'z' represents the integer value of z (122 ASCII value).
Fundamentals of strings
• Strings
– Array of characters treated as a single unit called
string:
• Can include letters, digits and special characters (*, /,
$)
– String literal (string constant) - written in double
quotes
• “Lovely Professional University."
Fundamentals of strings
• Strings are arrays of characters
– String is a pointer to first character (like array)
– Value of string is the address of first character
• Each element of the string is stored in a
contiguous memory locations.
• Terminated by a null character(‘\0’) which is
automatically inserted by the compiler to
indicate the end of string.
String Definition
• They are defined as
char array_name[size];
e.g. char carname[30];
or char *carname;
• It defines an array name and reserves 30 bytes
for storing characters and single character
consumes 1 bytes each.
• Since the last byte is used for storing null
character so total number of character specified
by the user cannot exceed 29.
String Initialization
• String Initialization
– Two ways:
– Define as a character array or a variable of type
char *
char color[] = "blue"; //char array
Or char color[] = { 'b', 'l', 'u', 'e', '\0' };
char *colorPtr = "blue"; //pointer variable
– Remember that strings represented as character
arrays end with '\0'
b •l coloruhas 5 eelements
\0 b l u e \0
color Temporary
*colorPtr
char *colorPtr = "blue"; //pointer variable
Printf(“%s”, colorPtr);
} //end main
Lovely
• So how to print:
Lovely Professional University
Standard I/O Library Functions
• List of functions in #include<stdio.h>
• Used for string input/output functions.
Function Description
gets( char *s ); Inputs characters from the standard input into the
array s until a newline or end-of-file character is
encountered. A terminating null character is
appended to the array.
puts( const char *s ); Prints the string s followed by a newline character.
#include <stdio.h>
Program to print
void main() strings with
{
white spaces
char name[100]; //string char array using library
puts(“\nEnter a string: ”); functions
gets(name); //to input string with space
printf(“\nString is: ”)
puts(name); //to output const string
}//end main
Enter a string:
Lovely Professional University
Output
String is:
Lovely Professional University
#include <stdio.h>
Program to print
void main() strings character
{
by character
char name[]={“Lovely Professional using loop.
University"}; //string char array
int i=0;
}//end main
strncmp(s1, s3, 6) = 0
strncmp(s1, s3, 7) = 1
strncmp(s3, s1, 7) = -1
Determining the length of string
strlen()
• Function strlen in #include<string.h>
• Function strlen() takes a string as an
argument and returns the number of
characters in the string
– the terminating null character is not included in
the length
#include <stdio.h>
#include <string.h>
Program
void main() demonstrates
{
/* initialize 3 char pointers */
string length
const char *string1 = function strlen()
"abcdefghijklmnopqrstuvwxyz";
const char *string2 = "four";
const char *string3 = "Boston";
printf("%s\"%s\"%s%d\n%s\"%s\"%s%d\n
%s\"%s\"%s%d\n",
"The length of “,string1,"is",
strlen(string1),
"The length of “,string2,”is“,
strlen(string2),
"The length of “,string3,”is“,
strlen(string3));
} /* end main */
The length of "abcdefghijklmnopqrstuvwxyz" is 26
The length of "four" is 4
The length of "Boston" is 6