Learning C/C++ Step-By-Step - Page 8
08. Step-by-Step C/C++ --- C Programming - Strings
Strings |
|
- Introduction | |
- Characteristics of a strings | |
- Operations on Strings | |
1. Definition of Strings 2. Initialization of Strings 3. Reading and printing of Strings 4. Reading Embedded Blanks 5. Length of a String 6. Strings and Functions 7. Array of Strings |
Introduction
Arrays are used to examine strings, generally strings are of array type variables.
A string is a collection of characters including space where as word is a collection of characters excluding space. Every string variable must be terminating with ‘\0’ null character and the index value is starts with 0.
Every string has the following characteristics:
1. It must be a collection of characters (i.e. characters, numbers, and special characters). 2. Every string must be ends with a NULL character ( i.e. ‘\0’ ) 3. A unique positive number called index identifies each character of a string. 4. Index value must be starts with 0. 5. Random access on characters in a string is possible. 6. A string must be declared with its fixed size like arrays. |
For Example consider the following example:
char str = " magic";
A variety of string library functions are used to manipulate strings. An array of strings is an array of arrays of type char.
Operations on Strings
We can perform much better operations than using Library string functions.
Strings can accept the following operations.
1. Definition of Strings 2. Initialization of Strings 3. Reading and printing of Strings 4. Reading Embedded Blanks 5. Length of a String 6. Strings and Functions 7. Array of Strings |
1. Definition of a String
Every variable must be declared at the beginning of the program.
Definition of string variable is as follows.
2. Initialization of Strings
Strings can be initialized in the following methods.
1. Direct Assignment
char name[10] = "Ashitha";Assigns "Ashitha" to name rest of the place left blank. |
2. Direct Assignment without Size
char name[] = "Ashitha";Assigns "Ashitha" to name and fix it’s width up to the size of Constant. |
|
3. Design time Assignment
char name[10]; But C never support the assignment like : name = "Ashitha"; |
4. Runtime Assignment
char name[10]; |
3. Reading and Printing Strings
C provides various types of string functions to read and print a string constant. Listed below.
Input Statements getch getche getchar gets scanf |
Output Statements putch putchar puts printf |
/* Program to accept and display a string */ /* 51_strings.c */ |
/* Program to accept and display a string with a prompt */
/* 52_strings.c */ |
4. Reading embedded blanks
scanf Accepts string, thus it will read strings consisting of a single word, but anything typed after a space is thrown away.
Eg. Enter String : Law is a bottomless pit.
You entered : Law
To read text containing blanks we use another function, gets().
/*read string with embedded blanks */ /* 53_gets.c */ |
5. Length of String
Every string has its fixed length depending on its constant.
The following program demonstrates, How to find the length of the string
/* To find the length of a given string */ /* 53_length.c */ |
6. Strings and Functions
A function is a self-contained block of statements that perform a specific task. The best way to organize strings.
The following are the example of string organization using functions.
/* Program to find the length of a string */ /* 54_len.c */
int main()
int len_str(char s[]) |
/* Program to accept and print a string */ /* 55_str.c */
int main()
void disp_str(char s[]) |
7. Array of Strings
Arrays are used to examine strings, generally strings are of array type variables. So, we can access array of strings.
The following examples illustrate, How Array of Strings organized.
/* Program to display an array of strings */ /* 56_display.c */ |
/* Program to accept and display an array of strings */ /* 57_strings.c */ |