Anda di halaman 1dari 21

Strings

13

Introduction to Programming
Strings

• In C, strings like “hello” are just arrays of


characters
• A string is terminated by the sentinel string
character ' \0 ', or null character which may
NOT be the last element in the array
• Null character: backslash-zero
• This is the only character whose bits are all
zeroes

Introduction to Programming
Implicit Strings

• Created by enclosing some text within double


quotation marks.
• Compiler creates a hidden char array to
represent the characters and ensures that a
'\0' is placed after the last character in the
string.
• ex.: printf(“Welcome\n”);

W e l c o m e \n \0

Introduction to Programming
Implicit Strings

• Why call it hidden array?


It has no name to which the program can refer
to.

• Why implicit?
The program does not have any control over
the size or number of elements in the array,
which was implicitly sized by the compiler.

Introduction to Programming
Variable/Explicit Strings

• Created by defining an explicit array of char


and then initializing it with other text.
ex.: char name[] = “Cao”;

• It is not necessary to state the storage


requirements or number of elements, since
this will be calculated by the compiler.

C a o \0

Introduction to Programming
Examples

char first_name[5] = {‘J’,’u’,’a’,’n’,’\0’};


– previous rules on arrays still apply

char last_name[3] = “Tamad”;


– gcc issues a warning but compiles anyway
– “Tam” is stored but not properly terminated
– No way of knowing when the actual string
really ends.

Introduction to Programming
Examples
char other[100] = “Maria Makiling”;
– specifying a larger size leaves space in the
array for other characters to be appended
– compiler still appends '\0' after the implicit
string

char characters[7] = “No null”;


– still valid though string is not properly
terminated.
– No way of knowing when to end.
Introduction to Programming
Printing Strings

• Strings can be printed per element as they


are just arrays:

int i;
char cool[] = “Programming”;
while(cool[i] != ‘\0’)
printf(“%c”, cool[i++]);

• Alternately, printf supports “%s”


printf(“%s \n”,cool);

Introduction to Programming
Printing Strings

• Also, we can use the function puts declared in


stdio.h
puts( cool );

• The use of puts() ALWAYS results in a new


line character '\n' being issued at the end of
each line of output.

Introduction to Programming
Accepting/Reading Strings
• Using scanf()
scanf( “%s”, cool );

• When using “%s”, the required address is the


address of the first element in the array

• The address operator '&' is not required


because the name of an array in C represents
the address of the first element in the array.

Introduction to Programming
Accepting/Reading Strings
• Same as
scanf( “%s”, &cool[0] );

• Note that the scanf function uses white space


characters to separate data into different
fields.

• We can use gets() declared in stdio.h to


permit a whole line to be read and does not
use white space characters to separate
various items of data entry.
Introduction to Programming
Example – scanf()

printf(“Anong pangalan mo? ”);
scanf(“%s”, string);
printf(“Ikaw si %s.”, string);
...

Anong pangalan mo? Wilmarc Lopez


Ikaw si Wilmarc.

Introduction to Programming
Example – gets()
...
puts(“Anong pangalan mo? ”);
gets( string );
printf(“Ikaw si ”);
puts( string);
...

Anong pangalan mo? Wilmarc Lopez


Ikaw si Wilmarc Lopez.

Introduction to Programming
Assigning to String

• Strings may be initialized with “=“ but not


assigned to with “=“

• Valid (initialization)
– char who[] = “Ser Wilmarc”;
• Invalid
– who = “Mom Wilmarc”;

Introduction to Programming
Assigning to String

#include <stdio.h>
#include <string.h>
main(void)
{
char who[20];
strcpy(who, “Manong Fishball”);
strcpy(who, “Tuition Fee Increase”);
printf(“%s”,who);

return 0;
}

Introduction to Programming
String Handling Functions in string.h
char s1[50] = “UP”;
char s2[50] = “UP System”;

String function Example Output/value

String length strlen(s1); 2


String strcat(s1,” Diliman”); s1 has the value
concatenate “UP Diliman”

String compare strcmp(s1,s2)

Introduction to Programming
String Handling Functions in string.h

strlen(s1) – returns the number of characters


before '\0' of s1 (either implicit or explicit)

char s1[50] = “UP”;


char s2[50] = “UP System”;
int length;
printf(“%d”, strlen(s1));
length = strlen(s2);

• What happens if s1 does not have a '\0'?


Introduction to Programming
String Handling Functions in string.h

strcpy(destination,source) – copies all of


the characters from source (either
implicit or explicit), including the '\0'
terminator to destination (explicit)
• Whatever exists in destination is overwritten
• It is assumed that destination has enough
space to hold the result
• The destination array is returned
source = strcpy(destination, “Hello”);

Introduction to Programming
String Handling Functions in string.h

strcat(s1,s2) – takes two strings (either


implicit or explicit) as arguments,
concatenates them and puts the
result in s1

strcpy(s1,“Hello”);
strcat(s1, “ world!”);
printf(“%s”,s1);

Introduction to Programming
String Handling Functions in string.h

strcmp(s1,s2) – an integer is returned


that is less than, equal to or greater
than zero, depending on whether s1 is
lexicographically less than, equal to,
or greater than s2, respectively.
– Both can either be implicit or explicit
strings.
strcmp(“Hello”,”Hell”); >0
strcmp(“Hell”, “Hello”); <0
strcmp(“Sat”,”Cat”); >0
Introduction to Programming
Lab Exercises

1) Write your own strcmp function. Name it my_strcmp.

2) Write your own strcpy dunction. Name it my_strcpy.

Introduction to Programming

Anda mungkin juga menyukai