Sie sind auf Seite 1von 12

COMP102 Prog.

Fundamentals: Strings / Slide 1

Strings

By
Prithiviraj.M
COMP102 Prog. Fundamentals: Strings / Slide 2

Character Strings
A sequence of characters is often referred to as a
character string.
A string is stored in an array of type char ending with
the null character '\0 '.
COMP102 Prog. Fundamentals: Strings / Slide 3

Strings are Character Arrays


Strings in C are simply arrays of characters.
Example: char s [10];
This is a ten (10) element array that can hold a
character string consisting of 9 characters.
This is because C does not know where the end
of an array is at run time.
By convention, C uses a NULL character '\0' to
terminate all strings in its library functions
COMP102 Prog. Fundamentals: Strings / Slide 4

Character Strings
COMP102 Prog. Fundamentals: Strings / Slide 5

What are Strings

The way a group of integers can be stored in an


integer array, group of characters can be stored in a
character array.

Character arrays or strings are used by programming


languages to manipulate text such as words and
sentences.
COMP102 Prog. Fundamentals: Strings / Slide 6

String Literals

String literals are given as a string quoted


by double quotes.
printf("Long long ago.");
Initializing char array ...
char s[10]="unix"; /* s[4] is '\0'; */
char s[ ]="unix"; /* s has five elements */
COMP102 Prog. Fundamentals: Strings / Slide 7

Printing with printf ( )


Example:
char str[ ] = "A message to display";
printf ("%s\n", str);

printf expects to receive a string as an additional


parameter when it sees %s in the format string

printf knows how much to print out because of


the NULL character at the end of all strings.
When it finds a \0, it knows to stop.
COMP102 Prog. Fundamentals: Strings / Slide 8

The C String Library


String functions are provided in an ANSI
standard string library.
Access this through the include file:
#include <string.h>
Includes functions such as:
Computing length of string
Copying strings
Concatenating strings

This library is guaranteed to be there in any


ANSI standard implementation of C.
COMP102 Prog. Fundamentals: Strings / Slide 9

strlen
strlen returns the length of a NULL terminated character string:
size_t strlen (char * str) ;
Defined in string.h
size_t
A type defined in string.h that is equivalent to an unsigned int
char *str
Points to a series of characters or is a character array ending with '\0'
The following code has a problem!
char a[5]={a, b, c, d, e};
strlen(a);
COMP102 Prog. Fundamentals: Strings / Slide 10

strcpy
Copying a string comes in the form:
char *strcpy (char * destination, char * source);
A copy of source is made at destination
source should be NULL terminated
destination should have enough room
(its length should be at least the size of
source)
The return value also points at the
destination.
COMP102 Prog. Fundamentals: Strings / Slide 11

Program to demonstrate printing of a string

main( )
{
char name[ ] = vishishth" ;
int i = 0 ;
while ( i <= 8 )
{
printf ( "%c", name[i] ) ;
i++ ;
}
}
COMP102 Prog. Fundamentals: Strings / Slide 12

Pointer program
main( )
{
char name[ ] = vishishth" ;
char *ptr ;
ptr = name ; /* store base address of string */
while ( *ptr != `\0' )
{
printf ( "%c", *ptr ) ; ptr++ ;
}
}

Das könnte Ihnen auch gefallen