Objectives:
To familiarize students with declaration, definition, and usage of Strings in C. Also to practice many Standard Library String Functions
Tools:
Turbo C IDE
Procedure:
Writing C program to perform the following tasks
• Declare and initialize string character array
• Copy one string into another using strcpy and strncpy standard Library String Functions
• Appends one string to the other string using strcatand strncatStandard Library String Functions
A string is a series of characters treated as a single unit. A string may include letters, digits, and various special characters such as +, -, *, / and $. String literals or string constants in C are written in double quotations marks.
A string in C is an array of characters ending in the null character (‘\0’). There is a null character at end of each and every string which indicates the ending or termination of a string. Format String specifier is %s in printf and scanf. The value of the string is the address of its first character.
Declaration and initialization of string can be done by using either a character array or a variable of type char pointer as shown below:
Char color[] = “blue”;
Char *colorptr = “blue”;
In which each initializes a variable to the string “blue”. The first declaration creates five-element array color containing the characters ‘b’, ‘l’, ‘u’, ‘e’, and ‘\0’. The second declaration creates a pointer variable colorptr that points to the string “blue” somewhere in memory.
The declaration and initialization of string char color[] = “blue” could also be written as:
char color[] = { ‘b’, ‘l’, ‘u’, ‘e’, ‘\0’ };.
Step 1: Create a New C Program File and save it as lab10.c
Step 2: Write the following code as shown in figures below
Figure 24 (a): Strings in C
Step 3: Save file again, compile and run it for required output as shown in the figure below
Figure 24 (b): Strings in C
No comments:
Post a Comment