Chapter 8: Character & String: in This Chapter, You'll Learn About
Chapter 8: Character & String: in This Chapter, You'll Learn About
Chapter 8: Character & String: in This Chapter, You'll Learn About
NI S1 2009/10
1
Principles of Programming
2
Principles of Programming
3
Principles of Programming
4
Principles of Programming
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",122);
return(0);
}
5
Principles of Programming
Example cont…
#include <stdio.h> #include <stdio.h>
void main(void) void main(void)
{ {
char ch; char ch;
printf("enter a character: "); printf("enter a character: ");
scanf("%c", &ch); scanf("%c", &ch);
if (ch >= 'A' && ch <= 'Z') if (ch >= 65 && ch <= (65+26))
{ {
printf("\ncapital letter\n"); printf("\ncapital letter\n");
} }
return (0); return (0);
} }
equivalent to
6
Principles of Programming
7
Prototype Function Descriptions
Example
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char loop = 'y';
char my_char;
if (isalpha(my_char))
{
printf("The character is an alphabet\n");
if (islower(my_char))
printf("and it is also a lower case alphabet\n");
if (isupper(my_char))
printf("and it is also an upper case alphabet\n");
}
if (isdigit(my_char))
printf("The character is a digit\n");
if (ispunct(my_char))
printf("The character is a punctuator\n");
fflush(stdin);
printf("\nanother character? [y = yes, n = no]: ");
loop = getchar();
printf("\n");
} return (0); 9
}
Principles of Programming
Example
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main(void)
{
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++;
}
return(0);
}
10
Principles of Programming
11
Principles of Programming
g r e e n \0
12
Principles of Programming
13
Principles of Programming
A r r i s \0 \0 \0 \0 \0
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
14
Principles of Programming
15
Principles of Programming
int i, count = 0;
17
Principles of Programming
18
Principles of Programming
19
Function Prototype Function Description
double atof (const char *nPtr) Converts the sting nPtr to double.
int atoi (const char *nPtr) Converts the string nPtr to int.
long atol (const char *nPtr) Converts the string nPtr to long int.
double strtod (const char *nPtr, Converts the string nPtr to double.
char **endptr)
long strtol (const char *nPtr, char Converts the string nPtr long.
**endptr, int base)
unsigned long strtoul (const char Converts the string nPtr to unsigned long.
*nPtr, char **endptr, int base)
Example
/*1. Converting a String Into an int Using atoi. */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
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));
return(0);
}
Output:
str1: 124
str2: -3
str3: 0
21
Principles of Programming
22
Principles of Programming
23
Principles of Programming
24
Principles of Programming
strcmp
25
Principles of Programming
Example: strcmp
#include <stdio.h>
#include <string.h>
int main(void)
{
char name[20] = "eddie";
char guess[20];
int correct = 1;
while(correct==1)
{
printf("Enter a name: ");
gets(guess);
strcat
27
Principles of Programming
Example 1: strcat
/* Concatenating Strings Using strcat */
#include <stdio.h>
#include <string.h>
str1: Hello World
int main(void) Press any key to continue
{
char str1[50] = "Hello ";
char str2[15] = "World";
strcat(str1, str2);
printf("str1: %s\n", str1);
return(0);
}
Note : This only works if you've defined the str1 array to be large
enough to hold the characters of the new string. If you don't
specify a size, the program will crash.
28
Principles of Programming
Example 2: strcat
/* Concatenating Strings Using strcat */
#include <stdio.h>
#include <string.h>
str2: WorldHello
int main(void) Press any key to continue
{
char str1[50] = "Hello ";
char str2[15] = "World";
strcat(str2, str1);
printf("str2: %s\n", str2);
return(0);
}
Note : This only works if you've defined the str1 array to be large
enough to hold the characters of the new string. If you don't
specify a size, the program will crash.
29
Principles of Programming
Example 3: strcat
/* Concatenating Strings Using strcat */
#include <stdio.h>
#include <string.h>
30
Principles of Programming
strncat
char *strncat ( char *dest, const char *src, size_t n );
strncat adds n characters from the second string to the
first string.
It returns a pointer to the concatenated string.
Make sure that the size of dest is large enough to hold the
entire contents of src as well as its own contents.
31
Principles of Programming
Example: strncat
/* Concatenating Strings Using strncat */
#include <stdio.h>
#include <string.h>
str1: Hello Wo
int main(void) Press any key to continue
{
char str1[50] = "Hello ";
char str2[15] = "World";
strncat(str1, str2, 2);
printf("str1: %s\n", str1);
return(0);
}
32
Principles of Programming
strcpy
char *strcpy ( char *dest, const char *src );
strcpy is short for string copy, which means it
copies the entire contents of src into dest. The
contents of dest after strcpy will be exactly the
same as src.
33
Principles of Programming
Example 1: strcpy
#include <stdio.h>
#include <string.h>
int main(void)
{
char string1[100] = “Malaysia";
char string2[50] = “Gemilang”;
strcpy(string1,string2);
printf(“string1: %s\n", string1);
printf(“string2: %s\n", string2);
return(0);
} string1: Gemilang
string2: Gemilang
Press any key to continue
34
Principles of Programming
Example 2: strcpy
#include <stdio.h>
#include <string.h> string2 has less
character than string1
int main(void)
{
char string1[100] = “Malaysia";
char string2[50] = “Boleh”;
strcpy(string1,string2);
printf(“string1: %s\n", string1);
printf(“string2: %s\n", string2);
return(0);
} string1: Boleh
string2: Boleh
Press any key to continue
35
Principles of Programming
Example 3: strcpy
#include <stdio.h>
#include <string.h> string1 has more
character than string2
int main(void)
{
char string1[100] = “Semenanjung ";
char string2[50] = “Malaysia”;
strcpy(string1,string2);
printf(“string1: %s\n", string1);
printf(“string2: %s\n", string2);
return(0);
}
string1: Malaysia
string2: Malaysia
Press any key to continue
36
Principles of Programming
strncpy
char *strcpy ( char *dest, const char *src, size_t n );
Strncpy copies the first n characters from the src into dest.
37
Principles of Programming
Example: strncpy
#include <stdio.h>
#include <string.h>
int main(void)
{
char string1[100] = "Malaysia";
char string2[50] = "Gemilang";
38
Principles of Programming
strlen
size_t strlen ( const char *s );
strlen will return the length of a string, minus the
null character ('\0'). The size_t is nothing to worry
about. Just treat it as an integer that cannot be
negative, which it is.
39
Principles of Programming
Example 1: strlen
#include <stdio.h>
#include <string.h>
int main(void)
{
char sentence[50] = "I love malaysia";
int i, count = 0;
count = strlen(sentence);
printf("%s has %d characters including the whitespace",
sentence, count);
return(0);
}
40
Principles of Programming
Example 2: strlen
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main(void)
{
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++;
}
return(0);
}
41
Principles of Programming
SUMMARY
C has a standard character-handling library that
includes some useful functions for testing types of
characters and for converting letters to uppercase
and lowercase.
String is another structured data type. C does not
support strings as a data type. But we can use
character arrays to represent strings.
Standard functions printf, puts
Standard functions scanf, gets
String manipulation functions => to copy strings, to
compare, to compute length, to concatenate
T.H.E E.N.D
43