Module-4 Even
Module-4 Even
STRINGS
What are Strings?
Recall that an array is a collection of elements of similar data-type that are stored in
contiguous memory locations. When the elements of an array are integers or floating-points,
the arrays are called numerical arrays. However, when the elements of an array are
characters, the arrays are called strings.
In otherwords, a string is an array of characters in C language.
OR
A String in C is one-dimensional character array where each element is either a character
constant or a string constant.
OR
A String is a series of characters treated as a unit.
Consider an example of storing the word, VOLVO in a array variable name[5]. The word
VOLVO contains five characters. Fig.1. shows the storing of the VOLVO word, as a string in
C language:
V O L V O \0
name[0] name [1] name[2] name[3] name[4] name[5]
A character array or a string stores a null (\0) character at the nth subscript position,
while a numerical array does not store such a character.
Difference between Characters and Strings
A character, written within single quotes is treated as character in C language. For
example, „A‟ is a character. On the other hand, a single or multiple characters written within
double quotes are treated as strings. For example, “A” is a string.
In C language, all the string are terminated by a special invisible character \0. Thus when
you write „A‟ single character A gets written into computer‟s memory. But when you write
“A” two characters A and \0 get stored in memory.
The printf( ) function along with the %s format specifier prints a string stored in
character array to the console. We can use the printf( ) function to display the string data with
the help of two ways. Either we can pass the string data directly within the printf( ) function
or we can store the string data in a character array, for instance str, and then pass the str array
as a parameter in the printf( ) function.
Example:- printf(“Test string”);
Or
Example:-char str[ ] = {“Test string”};
printf(%s”, str);
2. Using the puts( ) and gets( ) Functions
The gets( ) function reads a string from keyboard. This function stops reading the
string only when we press the Enter key from the keyboard. The gets( ) function is similar to
the scanf( ) function. The difference between the gets( ) and scanf( ) functions is that the gets(
) function can read the whole sentence, which is a combination of multiple words; while the
scanf( ) function can read the characters until a space or a newline character is encountered.
Program displaying the limitations of scanf( ) Function.
#include<stdio.h>
int main( ) {
char msg[70];
printf(“Enter a message”);
scanf(“%s”, msg);
printf(“%s”, msg);
return 0;
}
Output:
Enter a message
Welcome to C language
Welcome
The puts( ) function, on the other hand, prints a string or a value stored in a variable to
the console in the next line.
printf(“Enter a word”);
ch=getchar( );
putchar(ch);
return 0;
}
Output:
Enter a word
String
S
STRING Taxonomy
In C, a string can be stored in either fixed-length format or in variable-
length format as shown in fig. below.
STRINGS
Fixed Variable
Length Length
Length Delimited
Controlled
4. Delimited strings: In this format, the string is ended with a delimiter. The delimiter is
then used to identify the end of the string.
For ex. Char name[8];
V O L V O \0
*Note:- For more programming examples, refer text books and class notes.
Sunil Kumar B, Dept. of CSE, CITNC, Bengaluru. 6
Module 4 Strings and Pointers
Operations on Strings
String Handling/Manipulation Functions
The C language provides a string library that contains various predefined string handling
functions, such as strcat and strlen. These string handling functions are stored in the string.h
header file.
Finding the length of the string.
1. strlen( )
The strlen( ) function is used to find out the length of a string. The syntax to use the
strlen( ) function is as follows:
strlen(string_data);
Example:-
int n;
char st[20] = “BENGALURU”;
n = strlen(st);
This will return the length of the string 9 which is assigned to an integer variable n.
strcpy(city, “BENGALURU”);
This will assign the string “BENGALURU” to the character variable city.
POINTERS
Possible questions:-1 What is a Pointer? Explain how pointer variables are declared and
initialized.
A pointer is a variable which points or represents a storage location in
memory(RAM). In other words, a pointer is a variable which holds/stores the address of
another variable. Pointers are used to access the values stored in the memory locations using
the memory addresses.
Pointer declaration
A pointer is declared like a variable with appropriate data type. The pointer variable in the
declaration is preceded by the * (asterisk) symbol. It has the following form:-
type *var1, *var2,…….*var n;
A pointer variable is declared as follows:-
int *ptr, *x; /*declared integer pointers*/
float *xptr, *y; /* declared float pointers*/
char *p, *a[10]; / declared character pointers*/
Pointer initialization.
A pointer variable is initialzed as follows:-
int a, b; /*normal variables*/
ptr = &a; /*assigning the address of a and b to pointer*/
xptr = &b;
The symbol & (ampersand) is an address operator which is used to access the address of a
variable and assign it to a pointer to initialize it.
The symbol * (asterisk) is an indirection operator which is used to access the value of a
variable through a pointer.
Ex:- int m = 15, *mptr;
mptr = &m;
printf(“Value of m = %d”, *mptr); /* output is m = 15 */
note:- Write a small program on pointers.
location.
4. Since a new location is created, this 4. Since the existing memory location is
method is slow. used through its address, this method
is fast.
Pointer/Address Arithmetic
Possible questions:-1 Explain with an example how pointers can also be used on
arithmetic operators.
Like how the arithmetic operations are possible on normal variables, it‟s also possible to
perform arithmetic operations on pointer variables.
Ex:-Incrementation pointers
#include<stdio.h>
void main( )
{
char a;
char *p = &a;
printf(“Value of pointer p before incrementation is %u\n”,p); /* p = 15000 */
p++;
printf(“Value of pointer p after incrementation is %u\n”,p); /* p = 15001 */
}
QUESTION BANK
Sl. Strings RBL
No.
1 What are Strings? Explain how strings are declared and initialized? L1
2 Explain how strings are read and displayed? L2
3 Explain the function which helps in reading any string and displaying any L2
string.
4 Explain the various functions available in C to manipulate the strings with L2
suitable examples.
5 Explain string handling or string manipulation functions in C? L2
6 What are sting input and output functions? L1
7 What are arrays of strings? Demonstrate with an example program? L1
8. Write a „C‟ program which accepts Student Name by the user as input, L1
i. Prints the length of the name entered along with the name.
ii. Copy Student Branch using string copy function.
Print Student name Branch concatenated.
9. Write a „C‟ program to read strings including blank character and output the L1
same.
10. Write a „C‟ program to check whether the entered string is Palindrome or not. L1
Pointers
11 Explain storage classes in C with suitable example. L2
12 What is a Pointer? Explain how pointer variables are declared and initialized. L1
13 Explain how a pointer is used as an argument in a function? L2
14 Differentiate between call by value and call by reference with suitable L2
example program.
15 Write a C program to find the bigger of 2 numbers using pointers. L1
16 Develop a function in C that will swap (exchange) the value of two integer L3
variables passed as arguments. Also write the main program.