Wednesday

Strings in C


 Strings in C

A string is a series of characters in a group that occupy contiguous memory. A group of characters (Alphabets, digits and special characters) is called as a string.

Example 1:

“Thank you for visiting www.learnconline.com”
“www.learnconline.com is excellent site for beginners.”

A string should always be enclosed with in double quotes (").
If single quote is used then it is treated as character. Hence 'A' is different from "A". Every string ends with a null character ('\0'). Note that \0 is a single character. So when it has to be explicitly assigned to a string, it has to be written with in single quotes as '\0'.
A null character occupies 1 byte of memory.

Syntax:
char str[num_of_characters];

Example 2:
char name[20];

The above statement declares an array named “name” capable of holding 20 characters. The 20th character is null character. Hence we can store 19 characters. The 20th character is reserved for null character.

Example 3:
char name[15]=”LearnCOnline”;
Here,
‘L’ is stored at name[0]
‘e’ is stored at name[1]
‘a’ is stored at name[2]
‘r’ is stored at name[3]
.
.
.
.
‘e’ is stored at name[11]
And finally,
‘\0’ is stored at name[12].
name[14] and name[15] will contain garbage value.

Example 4:
char name[]=”Learn C Online”;
Here the size of an array is calculated automatically (15 characters).

We can also declare string as character pointer as follows:
char *name = “Learn C Online”;

Printing Strings:


Using Character pointer:
char *name = “Learn C Online”;
printf(name);
printf(“%s”, name);
printf(“%s”,&name[0]);

Using Character Array:
char name[]=”Learn C Online”;
printf(name);
printf(“%s”, name);
printf(“%s”,& name[0]);

printf function can be used to print a string. Similarly, puts function can be used to print a string as shown below.
char name[]=”Learn C Online”;
puts(name);

Accepting String as input:
We can use scanf or gets to accepts string from the user.

Using scanf:
scanf(“%s”,name);
The above statement passes the base address implicitly.

scanf(“%s”,&name[0]);
In the above statement, we are passing the address of the first element.

Using gets:
gets(name);
gets(&name[0]);

Although scanf and gets seems to be the same, there is a big difference between them. Let’s understand this difference.

Program using scanf:
char *str;
printf(“Enter the string: ”);
scanf(“%s”,str);
printf(“\n%s”,str);

Output:
Enter the string: Learn C Online
Learn

Program using gets:
char *str;
printf(“Enter the string: ”);
gets(str);
printf(“\n%s”,str);


Output:
Enter the string: Learn C Online
Learn C Online

When a string is accepted using scanf function, if a space is encountered then it will treat it as null character(‘\0’). In the above example, a space encountered after “Learn” is treated as null character. Hence while printing, only “Learn” is printed.
This problem can be resolved using gets function.

Kindly Bookmark this Post using your favorite Bookmarking service:
Technorati Digg This Stumble Stumble Facebook Twitter
YOUR ADSENSE CODE GOES HERE

0 comments:

Post a Comment

Dont Forget for Commets

 

| C programing tutorials © 2009. All Rights Reserved | Template Style by My Blogger Tricks .com | Design by Brian Gardner | Back To Top |