Bpops103 Module4
Bpops103 Module4
Chapter-13
STRING:
13.1 Introduction.
13.2 String taxonomy.
13.3 Operations on strings.
13.4 Miscellaneous string and character functions
13.5 Arrays of strings.
By-
Dr. SANTOSH K C
Assoc. Prof.
CSE, BIET
Davangere-04
13.1 Introduction.
• “A string is a sequence of characters enclosed
within double quotes”. or
• “String is an array of characters and terminated by
NULL character which is denoted by ‘\0’.
• In C, a string is a null-terminated character array. This
means that after the last character, a null character
('\0') is stored to signify the end of the character
array.
Figure below shows the difference between
character storage and string storage.
• Like we use subscripts (also known as index) to
access the elements of an array, we can also use
subscripts to access the elements of a string. The
subscript starts with a zero (0). All the characters of a
string are stored in successive memory locations.
Figure shows how str[] is stored in the memory.
• ASCII code of a character is stored in the memory
and not the character itself. So, at address 1000, 72
will be stored as the ASCII code for H is 72. The
statement char str[] = "HELLO";
• Syntax:
• the general form of declaring a string is
• char str[size];
• The other way to initialize a string is to initialize it as an
array of characters.
For example, char str[] = {'H', 'E', 'L', 'L', 'O', '\0'};
Here, the compiler will automatically calculate the
size based on the number of characters.
using scanf()
• Strings can be read using scanf() by writing scanf("%s", str);
• Unlike int, float, and char values, %s format does not
require the ampersand before the variable str.
• The main pitfall of using this function is that the function
terminates as soon as it finds a blank space. Therefore we
cannot read the complete sentence using scanf() function.
using gets()
• The string can be read by writing gets(str);
• gets() is a simple function that overcomes the
drawbacks of the scanf() function.
• gets() function is used to read a sequence of
characters (string) with spaces in between.
• The ‘gets()’ function allows us to read an ‘entire
line’ of input including whitespace characters.
Writing Strings
• Strings can be displayed on the screen using the
following three ways:
• using printf() function
• using puts() function, and
• using putchar() function repeatedly
using printf()
• Strings can be displayed using printf() by writing
printf("%s", str);
• We use the format specifier %s to output a string.
using puts()
• A string can be displayed by writing puts(str);
• puts() is a simple function that overcomes the
drawbacks of the printf() function.
• The puts() function writes a line of output on the
screen. It terminates the line with a newline
character (‘\n’).
13.2 String Taxonomy
• Fixed-length strings
• Variable-length strings
13.3 Operations on Strings
Finding Length of a String
• The number of characters in a string constitutes the
length of the string.
For example, LENGTH("C PROGRAMMING IS FUN")
will return 20. Note that even blank spaces are
counted as characters in the string.
Reversing a String
If S1="HELLO", then reverse of S1="OLLEH".
To reverse a string, we just need to swap the first
character with the last, second character with the
second last character, and so on.
Inserting a String in Another String
The insertion operation inserts a string S in the main text T
at the kth position. The general syntax of this operation
is INSERT(text, position, string).
• For example, INSERT("XYZXYZ",3, "AAA") = ? ?
Output:
The length of the string is: ??
strcpy ( ): String Copy
• The ‘strcpy()’ function copies the contents of source
string str2 to destination string str1 including ‘\0’.
• The strcpy() function copies characters from the
source string to the destination string until it finds null
character. It returns the argument str1.
Where,
• str1: it is the destination string
• str2: it is the source string.
Ex: Write a C program to demonstrate the usage of
strcpy().
#include<stdio.h>
#include<string.h>
void main( )
{
char str1[10], str2[10]= “BIET”;
strcpy(str1,str2);
printf(“The Source String=%s\n The Destination
String=%s”, str1,str2);
}
Output:
The Source String= BIET
The Destination String= BIET
strncpy(str1,str2,n): String Number Copy
• ‘strncpy()’ function is used to copy ‘n’ characters from
the source string str2 to the destination string str1.
Where,
• str1: it is the destination string.
• str2: it is the source string.
• n: n is the number of characters to be copied into
destination string.
Ex: Write a C program to demonstrate the usage of
strncpy().
#include<stdio.h>
#include<string.h>
void main()
{
char str1[10], str2[10]= “Computer”;
strncpy(str1,str2,3);
printf (“The Source String=%s\n The Destination
String=%s”, str1,str2);
}
Output:
The Source String= Computer
The Destination String= ??
strcmp(str1,str2): String Compare
• This function is used to compare two strings.
• The comparison starts with first character of each
string. The comparison continues till the
corresponding characters differ or until the end of
the character is reached.
Ex:
• “car” and “cat” are different strings. The characters
‘r’ and ‘t’ have different ASCII values. It returns
negative value since ASCII value of ‘r’ is less than
the ASCII value of ‘t’.
• “car” and “car” are same, the function returns 0.
By-
Dr. SANTOSH K C
Assoc. Prof.
CSE, BIET
Davangere-04
14.1 Introduction
“A pointer is a variable that holds the address of
another variable”. or
“A pointer is a variable that contains the memory
location of another variable”.
where,
• data_type: It can be int, float, char etc.
• Asterisk (*): It tells the compiler that we are declaring a
pointer variable.
• ptr_name: It is the name of the pointer variable.
• address_of_variable: It is the address of another variable.
Example:
int a; int a;
int *ptr; or int *ptr=&a;
ptr=&a;
14.4 Types of Pointer
14.4.1 Null Pointers
✓ Null pointer which is a special pointer value and does
not point to any value.
✓ This means that a null pointer does not point to any
valid memory address.
✓ To declare a null pointer, you may use the predefined
constant NULL which is defined in several standard header
files including <stdio.h>, <stdlib.h> , and <string.h>.
✓ After including any of these files in program, one can
write
int *ptr = NULL;
Uses of NULL Pointer in C
if (ptr == NULL)
{
Statement block;
}
14.4.2 Generic Pointers
✓ A generic pointer is a pointer variable that has void as
its data type.
✓ The void pointer, or the generic pointer, is a special
type of pointer that can point to variables of any data
type.
✓ It is declared like a normal pointer variable but using
the void keyword as the pointer’s data type.
For example,
void *ptr;
✓ Generic pointers are often used when you want a
pointer to point to data of different types at different
times.
Example:
#include <stdio.h>
void main()
{
int x=10;
char ch = ‘A’;
void *gp;
gp = &x;
printf("\n Generic pointer points to the integer value = %d",
*(int*)gp);
gp = &ch;
printf("\n Generic pointer now points to the character= %c",
*(char*)gp);
}
Output:
Generic pointer points to the integer value = 10
Generic pointer now points to the character = A
14.5 Passing Arguments to Function
Using Pointers
• Call-by-value
• Call by reference
Write a C program to add two numbers using call by
reference
#include<stdio.h>
int add (int *a, int *b)
{
int sum;
sum = *a + *b;
return sum;
}
void main()
{
int a,b, res;
printf(“Enter the values of a and b:”); scanf(“%d%d”,&a,&b);
res = add(&a,&b);
printf(“result =%d\n”, res);
}
To be continued…