Академический Документы
Профессиональный Документы
Культура Документы
Strings
CSEB113 PRINCIPLES of
PROGRAMMING
by
Badariah Solemon
BS (May 2013)
Topics
1. More about characters
2. Fundamentals of strings
Topic 1
MORE ABOUT CHARACTERS
BS (May 2013)
Characters
Recall from Chapter 3 that characters in C consist of
any printable or nonprintable character in the
computers character set including lowercase letters,
uppercase letters, decimal digits, special characters
and escape sequences.
A character is usually stored in the computer as an 8bits (1 byte) integer.
The integer value stored for a character depends on
the character set used by the computer on which the
program is running.
Two commonly used character sets:
ASCII (American Standard Code for Information
Interchange)
EBCDIC (Extended Binary Coded Decimal Interchange
Code)
BS (May 2013)
BS (May 2013)
Example1
#include <stdio.h>
void main(void)
{
char my_A = 'A';
char my_Z = 'Z';
char my_a = 'a';
char my_z = 'z';
printf("\nASCII
printf("\nASCII
printf("\nASCII
printf("\nASCII
ASCII
ASCII
ASCII
ASCII
value
value
value
value
for
for
for
for
A
Z
a
z
is
is
is
is
65
90
97
122
65 in ASCII represents A
90 in ASCII represents Z
97 in ASCII represents a
122 in ASCII represents z
value
value
value
value
for
for
for
for
A
Z
a
z
is
is
is
is
%d", my_A);
%d",my_Z);
%d", my_a);
%d",my_z);
printf("\n");
printf("\n65 in ASCII represents %c",65);
printf("\n90 in ASCII represents %c",90);
printf("\n97 in ASCII represents %c",97);
printf("\n122 in ASCII represents %c\n",122);
BS (May 2013)
Example2
#include <stdio.h>
#include <stdio.h>
void main(void)
{
char ch;
void main(void)
{
char ch;
printf(Enter a character: ");
scanf("%c", &ch);
Equivalent
BS (May 2013)
Functions in <ctype.h>
Functions
Descriptions
int isdigit(int c)
int isalpha(int c)
int isalnum(int c)
int isxdigit(int c)
int islower(int c)
int isupper(int c)
int tolower(int c)
int toupper(int c)
int isspace(int c)
Returns true if c is a white space character newline (\n), space ( ), form feed
(\f), carriage return (\r), horizontal tab (\t) or vertical tab (\v) and 0
otherwise.
int iscntrl(int c)
int ispunct(int c)
Returns a true if c is a printing character other than a space, a digit or a letter, and
0 otherwise.
int isprint(int c)
int isgraph(int c)
int isdigit(int c)
10
Example
Enter a character: G
#include <ctype.h>
The character is an alphabet
void main(void)
and it is also an upper case alphabet
{
char loop = 'y, myChar;
Another character? [y = yeas, n = no]:y
while (loop == 'y' || loop == 'Y')
{
Enter a character: 50
fflush(stdin);
The character is a digit
printf("Enter a character: ");
Another character? [y = yeas, n = no]:n
myChar = getchar();
if (isalpha(myChar))
{
printf("The character is an alphabet\n");
if (islower(myChar))
printf("and it is also a lower case alphabet\n");
if (isupper(myChar))
printf("and it is also an upper case alphabet\n");
}
if (isdigit(myChar))
printf("The character is a digit\n");
if (ispunct(myChar))
printf("The character is a punctuator\n");
fflush(stdin);
printf("\nanother character? [y = yes, n = no]: ");
loop = getchar();
printf("\n");
}
BS (May 2013)
11
}
Topic 2
FUNDAMENTALS OF STRINGS
BS (May 2013)
12
What is A String
A string (or character string) is simply a sequence
of characters such as persons name, address,
city and etc.
Just like any other data types, string may be read,
processed and displayed. However, string is not a
data type.
A string in C is a special kind of 1-D array of
characters ending with the null character (\0).
It is written inside a double quotation mark (" ")
A string
may
be assign
in a declaration to either a
char
color[6]
= green;
char color[]
green
char array
or to a= char
pointer as follows:
char *color = green
BS (May 2013)
13
Strings Technically
Given this declaration:
char color[6] = green;
color g
14
Initializing A String
We can initialize string variables at compile
time such as:
char name1[10] = Arris;
So:
char myDrink[3] = tea;
(the
size of the string
+ 1) to
char
myDrink[4]
= tea;
15
16
The following:
We are closed on Tuesday
is what I received.
17
18
the
}
I love Malaysia has 15 characters including the whitespace
BS (May 2013)
19
#include <stdio.h>
#include <string.h>
void main(void)
{
char name[ ] = "Mona Lisa";
int x=0;
x = strlen(name);
printf("There are %d characters in the name. ",
x);
}
There are 9 characters in the name.
BS (May 2013)
20
Topic 3
STANDARD OPERATIONS ON
STRINGS
BS (May 2013)
21
Operations on Strings
The standard C library comes with a long list
of functions to perform operations on strings.
To use the functions, include the :
#include <string.h> in your program.
The standard functions covered in this
course include:
Copying strings with strcpy() and strncpy()
Comparing strings with strcmp() and strncmp()
Concatenating strings with strcat() and
strncat()
BS (May 2013)
22
initial = B;
name=Badariah Solemon;
initial = B;
name=Badariah Solemon;
For
example:
#include
<string.h>
strcpy(copy,msg);
printf(The copied message: %s, copy);
BS (May 2013)
23
#include <string.h>
The second copy gave: The game is
#include <stdio.h>
void main (void){
Observe
this example:
char copy[25], msg[25]=The game is underway;
strcpy(copy, msg);
printf(The first copy gave: %s, copy);
24
Example:
Difference 1: -1
Difference 2: 1
Difference 3: 0
25
#include <string.h>
#include <stdio.h>
Example: void main (void)
{
char first[]="April", second[]="September";
int diff=0;
Match found.
diff=strncmp(first,"AprIL",3);
if(diff==0)
printf("Match found.");
else
printf("There is no match");
BS (May 2013)
26
Example:
First string:
HelloWorld
#include <string.h>
void main(void)
{
char str1[20] = "Hello", str2[] = "World";
strcat(str1, str2);
printf(First string:\n%s\n", str1);
}
BS (May 2013)
27
28
Example output:
Enter your name: badariah
Enter your phone number: 0123453862
Your user ID code is ba0123
BS (May 2013)
29
Strings Conversion
If you have a string of digits such as below:
char hello[]
= 9999;
you may convert the string to integer and floatingpoint values by using strings conversion
functions.
= 9999;
30
Purpose
nPtr
- The pointer to the string to be converted.
endptr - The pointer to which the remainder of the string will be
assigned after the conversion. We can pass a NULL if the remaining
string is to be ignored.
base
- Indicates the format (base) of the string to be converted. If 0 is
given, that means the value to be converted can be in octal (base 8),
decimal (base 10) or hexadecimal
(base
16).
BS (May
2013)
31
Example
//1. Converting a String Into an int Using atoi.
#include <stdio.h>
#include <stdlib.h>
void main()
{
char str1[] = "124z3yu87";
char str2[] = "-3.4";
char str3[] = "e24";
printf("str1: %d\n", atoi(str1));
printf("str2: %d\n", atoi(str2));
printf("str3: %d\n", atoi(str3));
}
str1: 124
str2: -3
str3: 0
BS (May 2013)
32
Character Handling in A
String
#include <ctype.h>
#include <string.h>
Enter a string: hello malaysia 12 pagi
void main(void)
Hello malaysia 12 pagi has 17 alphabet, 2 digit and 3 space
{
char string[50];
int length, i, alpha = 0, digit = 0, space = 0;
printf("Enter a string: ");
gets(string);
length = strlen(string);
for (i = 0; i < length; i++)
{
if (isalpha(string[i]))
alpha++;
if (isdigit(string[i]))
digit++;
if (isspace(string[i]))
space++;
}
printf("%s has %d alphabet, %d digits and %d spaces
\n",string,alpha,digit,space);
}
BS (May 2013)
33
BS (May 2013)
34
Summary
1. More about characters
2. Fundamentals of strings
35