article

Character and Strings

Email
Submitted on: 1/1/2015 5:05:00 PM
By: Indee (from psc cd)  
Level: Beginner
User Rating: By 5 Users
Compatibility: C
Views: 734
 
     This is in continuation of tutorials I am writing about C. Todays topic is Character and Strings. Experts please skip this tutorials.

 
				Tutorial-Characters and Strings

CHARACTER AND STRINGS


Ok atlast after a long gap I am writing the third tutorial explaining the concept of C language. Today's topic is character and strings. Experts skip this articles.

Characters

Another very imortant data in c is char, which stands for character. A character is a 1-byte value that can be used to hold printable character or integers in the range -128 to +127. A character constant is enclosed between single quotes. Ok let's see a sample program.

A Simple Example using characters

#include

/* Sample Program */

void main ()
{
char ch;

char = 'A';
printf(" %c", ch);

ch = 'B';
printf("%c", ch);

ch = 'C';
printf("%c", ch);

}
Output of this program will be: ABC


Although it is possible to use scanf() to read a single character from the keyboard, a more common way is to use library function getche(). The getche() function waits until a key is pressed and then returns the result. Let see another sample program. This program will display "you pressed my magic key" if I is pressed. (Tip: another use of getche() function is to hold the output displayed on the screen until a key is pressed.) This program also illustrates that character can be used in if statements.

Another Simple Example using characters

#include

/* Sample Program */

void main ()
{
char ch;

ch = getche(); /* read one character from the keyboard */
if(ch== 'I')
printf("you pressed my magic Key\n");

}


Strings

In C, a string is an array of characters terminated by a null.(Tip: in C, a null is essentially the same as 0.)C does not have a "string" type. Instead, it is declared as an array of characters and use the various string function found in the library to manipulate them. Array will come later in my tutorials but a few basic principles are required here.

Well array can be of number of dimensions but for this tutorial I am going to stick to one-dimensional array. A one-dimensional array is a list of variables of the same type. We can create such an array by placing the size of the array, enclosed between square brackets, after the array name. For example following fragment declares an 80-element character array called str.

char str[80];

To reference a specific element, place the index between square brackets after the array name. All arrays in C are indexed from zero. Therefore, str[0] is the first element, str[1] is the second element, with str[79] being the 80th and last element.

It is most important to remember that no bounds-checking performed. If not careful, it is possible for program to "run off the end" of an array. A simple method of preventing this is to always use an array that is large enough to hold whatever will be put into it. Remember that all strings end in a null. Therefore array must be atleast one character larger than the largest string so that it can accommodate the null terminator. A null in C is specified as the character constant '\0'. As an example, a character array large enough to hold the word"hello" will need at least six characters long, five for the string and one for the null terminator, as shown below:

hello'\0'

To read a string from the keyboard, first create a character array to hold the string and then use the library function gets(). The gets() function takes the name of the string as an argument and reads characters from the keyboard . let's see an example program.

Simple Example using strings

#include
#include

/* Sample Program */

void main ()
{
char str[80];

printf("Enter your name: ");
gets(str);
printf("hello %s", str);
}


Notice that the format command %s is used to cause printf() to print a string.

There are several standard functions used to manipulate strings. The two most common are strlen(), which returns the length of a string, and strcpy(), which copies a string from one string to another. They have the syntax strlen(string) and strcpy(destination, source) respectively. Copying a larger string to a smaller string, or reading a string into variable that is not large enough to store it can cause unpredictable results. Always check that the destination string is large enough to accommodate the source string. Let see an example showing these two library functions.

Another Simple Example using strings

#include
#include

/* Sample Program */

void main ()
{
char str[8];
char newstr[8];

printf("Please enter your name: ");
gets(str);
printf("Hello %s.\n", str);
printf("There are %d letters in your name.\n",strlen( str));
strcpy(newstr, str);
printf("newstr is %s.\n", newstr);
}


Ok guys! thats enough for this tutorial next tutorial will be about OPERATORS IN C

Bye for now!!


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.