Anda di halaman 1dari 5

Chapter 7

Strings Character Constants


Okay, let's recap. Each character has a numerical value. To find the numerical value, run the small program back in the DATA TY E! section. A character constant is "ust a single character, enclosed in single #uote marks and can be assigned to variables of the int or char data types.

Character Arrays
$f you %ere to have an array of characters &$T'O(T the null character )\0 * backslash and +ero,, you'd have an ordinary character array, rather than a string constant.

String Constants
-ut if you declare an array of the char data type and you include the null character, you'd have a string constant. Alternatively, you can initiali+e a character array %ith a string constant by enclosing the string %ith double #uote marks but %ithout the null character * the compiler %ill automatically kno% it's a string if it's enclosed in double #uote marks. Also, a string is considered to be a pointer variable of the char data type. -ut a character constant cannot be assigned to a pointer variable. An e.ample that sho%s the / cases listed above0
#include <stdio.h> int main() { int a = 'H'; char b 3 'i'4

12 character constants 21

printf)5The numeric value of a is 6d7n5, a,4 printf)5The numeric value of b is 6d7n7n5, b,4 printf)5The character that a holds is 6c7n5, a,4 printf)5The character that b holds is 6c7n5, b,4 char array89:; 3 <'-','e','e','r'=4 12 a character array 21 char array>9?; 3 <'@','o','f','f','e','e','7A'=4 12 a string constant 21 12 don't forget to add an e.tra element for the null characterB 21
@opyright Cogic Option vt. Ctd.

char array/9; 3 5Or you can specify an unsi+ed char array54 char 2array:4 array: 3 5 "ust to be safe.54 12 printf)5array8 holds 6s7n5, array8,4 is illegal * array8 DOT a string 21 printf)57narray8 holds07n7t5,4 for)int i3A 4 iE: 4 iFF, printf)56c5, array89i;,4 12 but is a character array 21 printf)57narray> holds07n7t6s7n5, array>,4 printf)5array/ and array: holds07n7t6s6s7n5, array/, array:,4 return A4 =

Reading Strings
One possible %ay to read in a string is by using scanf. 'o%ever, the problem %ith this is that if you %ere to enter a string %hich contains one or more spaces, scanf %ould finish reading %hen it reaches a space or if return is pressed, therefore part of the string %ould be chopped off. !o, to be safe, use the gets function. $t takes "ust one argument and that is a pointer to the string )in other %ords, the name of the character array but don't forget to declare the array 1 pointer variable firstB,. &hat's more, is that it automatically prints out a ne%line character, making the output a little neaterB

Writing Strings
'ere it is possible to use printf and the string format specifier, %hich you've already seen me done in the past. &hat is the puts functionG * it's similar to gets is that it takes one argument and that is a pointer to the string. This also automatically adds a ne%line character after printing out the string. !ometimes this can be a disadvantage, so printf could be used instead. -ut, as %ith printf and scanf, if you %ant to use gets and puts you'd have to include the stdio.h header file. E.ample time0
#include <stdio.h> int main() { char arra !"#0$;
@opyright Cogic Option vt. Ctd.

>

char arra %"#0$; printf(&'o( enter another string less than #0 characters (ith spaces) \n&); gets(arra %); printf(&\n*ou entered) &); puts(arra %); printf(&\n+r entering a string less than #0 characters, (ith spaces) \n&); scanf(&-s&, arra !); printf(&\n*ou entered) -s\n&, arra !); . return 0;

Try this e.ample * it %ill compile okay, but try and see %hat happens %hen you enter a > %ord string in your first attempt.... -etter avoid using scanf to read in strings ehGBB
#include <stdio.h> int main() { char arra !"#0$; char arra %"#0$; printf(&+r entering a string less than #0 characters, (ith spaces) \n&); scanf(&-s&, arra !); printf(&\n*ou entered) -s\n&, arra !); printf(&'o( enter another string less than #0 characters (ith spaces) \n&); gets(arra %); printf(&\n*ou entered) &); puts(arra %); . return 0;

Other Functions
The follo%ing functions are used to manipulate strings. -ut you'd have to include the string.h header file. $f you ever %anted to find out ho% long a string is %ithout counting the number of characters manually, you could use the strlen function. $t takes one argument, %hich is a pointer to the string )or the character array name,, and returns the number of characters in the string but DOT $D@C(D$DH the null character, %hich is not displayed.
@opyright Cogic Option vt. Ctd.

To copy a string to another string variable, you could use the strcp function. This takes > arguments, both of %hich are pointers )or character array names, if you like,. The first one is %here you'd like the string copy TO. The second is %here the string %ill be copied IJOK. 'ave a look0
#include <stdio.h> #include <string.h> int main() { char arra !"#0$; char arra %"#0$ = &/oring0&; int si1e; printf(&2nter a string less than #0 characters) \n&); gets(arra !); si1e = strlen(arra !); printf(&\n*our string is -d 3 t-s long...\n&, si1e, (si1e==! 4 &e& ) &es&)); printf(& ... and contains -d characters\n\n&, si1e); printf(&/efore cop ing, arra %"$ contains ''-s''\n&, arra %); strcp (arra %, arra !); printf(&'o( arra %"$ contains ''&); puts(arra %); printf(&''\n&); . return 0;

A Word About The Null Character


Every non +ero value in a string is considered as logical TJ(E. The null character is considered logical IAC!E. This makes it possible to print out a string using loops0
#include <stdio.h> #include <string.h> int main() { char arra !"$ = &+ired and &; char arra %"$ = &e5hausted at 6)07 89 0&; int i = 0; (hile(arra !"i$0='\0') {
@opyright Cogic Option vt. Ctd.

printf(&-c&, arra !"i$); i::; . for(i=0 ; arra %"i$ ; i::) printf(&-c&, arra %"i$); return 0; .

The condition in the %hile loop is more readable than in the for loop. Dote that the null character is enclosed in single #uote marks * this is because it's considered as a character constant and %e're evaluating character constants as %e iterate through each element of the array. $n the for loop, one iteration is performed if the e.pression in the middle returns logical TJ(E. All the elements in character arrays return logical TJ(E e.cept for the null character. Therefore the for loop %ill stop iterating once the null character )i.e. the end of the string, is reached. One final remark is that in the second array in the e.ample, the +ero returns logical TJ(E. &hyG -ecause it's a character constant and not an actual number.

@opyright Cogic Option vt. Ctd.

Anda mungkin juga menyukai