Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Fundamentals of Strings and Characters The difference between an integer digit and a character digit Character handling library String conversion functions Standard input/output library functions String manipulation functions
char num = 1 and char num = 1 are not the same. char num = 1 is represented in the computer as 00000001. char num = 1 on the other hand is number 49 according to the ASCII character set. Therefore, it is represented in the computer as 00110001.
printf("\nASCII value for A is %d", my_A); printf("\nASCII value for Z is %d",my_Z); printf("\nASCII value for a is %d", my_a); printf("\nASCII value for z is %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",122); }
Principles of Programming - NI2005 5
Sample output
ASCII value for A is 65 ASCII value for Z is 90 ASCII value for a is 97 ASCII value for z is 122 65 in ASCII represents A 90 in ASCII represents Z 97 in ASCII represents a 122 in ASCII represents z
Example cont
#include <stdio.h> void main(void) { char ch; printf("enter a character: "); scanf("%c", &ch); if (ch >= 'A' && ch <= 'Z') { printf("\ncapital letter\n"); } } #include <stdio.h>
equivalent to
Principles of Programming - NI2005 7
A string is accessed via a pointer to the first character in the string. In memory, these are the characters stored:
r e
\0
10
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
11
12
Use scanf function together with the format specifier %s for interactive input string. (no whitespace character) If the string to be read as an input has embedded whitespace characters, use standard gets function.
14
15
Example cont
/* Sample output */ Enter a string less than 50 characters with spaces: hello world You have entered: hello world Try entering a string less than 50 characters, with spaces: hello world You have entered: hello
16
17
Prototype 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)
Function Descriptions Returns a true if value c is a digit, and 0 (false) otherwise. Returns a true if value c is a letter, and 0 otherwise. Returns a true if value c is a digit or a letter, and 0 otherwise. Returns a true value if c is a hexadecimal digit character, and 0 otherwise. Returns a true value if c is a lowercase letter, and 0 otherwise. Returns a true value if c is an uppercase letter, and 0 otherwise. If c is an uppercase letter, tolower returns c as a lowercase letter. Otherwise, tolower returns the argument unchanged. If c is a lowercase letter, toupper returns c as an uppercase letter. Otherwise toupper returns the argument unchanged. 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. Returns a true if c is a control character, and 0 otherwise. Returns a true if c is a printing character other than a space, a digit or a letter, and 0 otherwise. Returns a true value if c is a printing character including space ( ), and 0 otherwise. Returns a true value if c is a printing character other than space ( ), and 0 otherwise.
Principles of Programming NI2005 18
19
Function Prototype double atof (const char *nPtr) int atoi (const char *nPtr) long atol (const char *nPtr) double strtod (const char *nPtr, char **endptr) long strtol (const char *nPtr, char **endptr, int base) unsigned long strtoul (const char *nPtr, char **endptr, int base)
Function Description Converts the sting nPtr to double. Converts the string nPtr to int. Converts the string nPtr to long int. Converts the string nPtr to double. Converts the string nPtr long. Converts the string nPtr to unsigned long.
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).
Principles of Programming NI2005 20
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)); }
21
int putchar(int c) int puts(const char *s) int sprintf(char *s, const char *format, ) int sscanf(char *s, const char *format, )
22
23
24
Example
int strcmp ( const char *s1, const char *s2 );
strcmp will accept two strings. It will return an integer. This integer will either be:
Negative value if s1 is less than s2. Zero value if s1 and s2 are equal. Positive value if s1 is greater than s2.
strcmp is case sensitive. strcmp also passes the address of the character array to the function to allow it to be accessed.
25
Example cont
char *strcat ( char *dest, const char *src );
strcat is short for string concatenate, which means to add to the end, or append. It adds 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.
26
Example cont
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.
27
Example: strcpy
#include <stdio.h> #include <string.h> void main() { char string1[100] = Malaysia"; char string2[50] = Gemilang; strcpy(string1,string2); printf(string1: %s\n", string1); printf(string2: %s\n", string2); }
Output :
string1: Gemilang string2: Gemilang
28
Example: strncpy
#include <stdio.h> #include <string.h>
Example: strcmp
#include <stdio.h> #include <string.h> void main() { char name[20] = "EDDIE"; char guess[20]; int correct = 0; while(correct==0) { printf("Enter a name in uppercase: "); gets(guess); if(strcmp(name, guess)==0) { printf("Correct!\n"); correct = 1; } else printf("Try again: \n"); } }
Principles of Programming - NI2005 30
Example cont
You can only exit the program by entering "EDDIE". To perform a lowercase string comparison, use stricmp instead of strcmp but be warned: stricmp is NOT an ANSI C so it won't be supported by all C compilers.
31
Example: strcat
/* Concatenating Strings Using strcat */ #include <stdio.h> #include <string.h> void main() { char str1[50] = "Hello "; char str2[ ] = "World"; strcat(str1, str2); printf("str1: %s\n", str1); } Output: str1: Hello World Note : This only works if you've defined the str1 array to be large enough to hold the characters of your string. If you don't specify a size, the program may crash.
Principles of Programming - NI2005 32
Example: strncat
/* Concatenating Strings Using strncat */ #include <stdio.h> #include <string.h> void main() { char str1[50] = "Hello "; char str2[ ] = "World"; strncat(str1, str2, 2); printf("str1: %s\n", str1); } Output: str1: Hello Wo
33
Example: strlen
#include <stdio.h> #include <string.h> void main(void) { char sentence[ ] = "I love malaysia"; int i, count = 0; count = strlen(sentence); printf("%s has %d characters including the whitespace", sentence, count); }
Sample output: I love Malaysia has 15 characters including the whitespace
Principles of Programming - NI2005 34
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
Principles of Programming - NI2005 36