Friday, July 10, 2009

Character using Arrays in C programming Language

Character Arrays

A string constant , such as
 "I am a string"
is an array of characters. It is represented internally in C by the ASCII characters in the string, i.e., ``I'', blank, ``a'', ``m'',... for the above string, and terminated by the special null character ``\0'' so programs can find the end of the string.

String constants are often used in making the output of code intelligible using printf ;

 printf("Hello, world\n");
printf("The value of a is: %f\n", a);

String constants can be associated with variables. C provides the char type variable, which can contain one character--1 byte--at a time. A character string is stored in an array of character type, one ASCII character per location. Never forget that, since strings are conventionally terminated by the null character ``\0'', we require one extra storage location in the array!

C does not provide any operator which manipulate entire strings at once. Strings are manipulated either via pointers or via special routines available from the standard string library string.h. Using character pointers is relatively easy since the name of an array is a just a pointer to its first element. Consider the following code:



void main()
{
char text_1[100], text_2[100], text_3[100];
char *ta, *tb;
int i;
/* set message to be an arrray */
/* of characters; initialize it */
/* to the constant string "..." */
/* let the compiler decide on */
/* its size by using [] */
char message[] = "Hello, I am a string; what are you?";

printf("Original message: %s\n", message);

/* copy the message to text_1 */
/* the hard way */
i=0;
while ( (text_1[i] = message[i]) != '\0' )
i++;
printf("Text_1: %s\n", text_1);

/* use explicit pointer arithmetic */
ta=message;
tb=text_2;
while ( ( *tb++ = *ta++ ) != '\0' )
;
printf("Text_2: %s\n", text_2);

}


The standard ``string'' library contains many useful functions to manipulate strings; a description of this library can be found in an appendix of the K & R textbook. Some of the most useful functions are:
char *strcpy(s,ct)     -> copy  ct into s, including ``\0''; return s
char *strncpy(s,ct,n) -> copy ncharcater of ct into s, return s
char *strncat(s,ct) -> concatenate ct to end of s; return s
char *strncat(s,ct,n) -> concatenate n character of ct to end
of s, terminate with ``\0''; return s
int strcmp(cs,ct) -> compare cs and ct; return 0 if cs=ct,
<0 and="">0 if cs>ct
char *strchr(cs,c) -> return pointer to first occurence of c
in cs or NULL if not encountered
size_t strlen(cs) -> return length of cs
(s and t are char*, cs and ct are const char*, c is an char converted to type int, and n is an int.)

Consider the following code which uses some of these functions:



#include <>

void main()
{
char line[100], *sub_text;
/* initialize string */
strcpy(line,"hello, I am a string;");
printf("Line: %s\n", line);
/* add to end of string */
strcat(line," what are you?");
printf("Line: %s\n", line);
/* find length of string */
/* strlen brings back */
/* length as type size_t */

printf("Length of line: %d\n", (int)strlen(line));

/* find occurence of substrings */
if ( (sub_text = strchr ( line, 'W' ) )!= NULL )
printf("String starting with \"W\" ->%s\n", sub_text);

if ( ( sub_text = strchr ( line, 'w' ) )!= NULL )
printf("String starting with \"w\" ->%s\n", sub_text);

if ( ( sub_text = strchr ( sub_text, 'u' ) )!= NULL )
printf("String starting with \"w\" ->%s\n", sub_text);

}



Other Links



1. A First Program

2. Let's Compute

3. Loops

4. Symbolic Constants

5. Conditionals

6. Pointers

7. Arrays

8. Character Arrays

9. I/O Capabilities

10. Functions

11. Command-line Arguments

12. Graphical Interfaces: Dialog Boxes

No comments:

Blog List