POP Module 4
POP Module 4
POP Module 4
Strings :
enclosed in double quotes that will be treated as a single data element followed by a null
character ‘\0’
Syntax:
char string_name[size];
Where,
char is the data type of strings
string_name is a valid identifier which is the name for the string variable size indicates the length
of the string which is to be stored
Initialization of strings:
if we specify the characters separately we should use ‘ ’, for each character and finally enclosing
all the characters within { }
The string initialized previously will be stored in memory as
Reading a String:
If we declare a string by writing
Char str [100];
Then str can be read by using three ways:
1. Using scanf() function
2. Using gets() function
3. Using getchar() , getch(), getche() function repeatedly
scanf() function
We do not use “&” symbol in scanf because string name itself represent the address where it is to
be stored.
%s is the format specifier used for string.
Although the syntax of scanf() fucntion is well known and easy to use, the main pitfall with this
function is that the function terminates as soon as it finds a blank space.
For example, if the user enters Hello World, then strr will contain only Hello. This is because the
moment a blank space is encountered, the string is terminated by the scanf() function.
gets() function
Strings can be read by using gets() by writing:
gets (str);
gets() is a simple function that overcomes the drawbacks of scanf(). The gets() function takes the
starting address oft he string which holds the input. The string inputted using gets() is
automatically terminated with a null character.
getchar() function
Strings can be read by using gets() by writing:
getchar ( );
Strings can also be read by calling the getchar() function repeatedly to read a sequence of single
characters (unless terminating character is entered) and simultaneoulsy storing it in a character
array.
Example:
To raead characters of string using getchar () function.
i=0;
ch = getchar();
while ( ch != ‘ \0 ‘ )
{
str [ i ] = ch;
i++;
ch = getchar();
}
str [ i ] = ‘\0‘;
Writing a String:
Strings can be displayed on screen using three ways:
1. Using printf() function
2. Using puts() function
3. Using putchar() function repeatedly
Printf ( ) function
String can be displayed using pritnf () by writing
puts() function
String can be displayed using puts () 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 characetr ("\n").
putchar() function
String can be displayed using putchar () by writing
putchar( );
putchar() function is repeatedly called to display or print the sequence of single charaters.
Example:
i=0;
while ( str [i] != ‘ \0 ‘ )
{
putchar(str[i]);
i++;
}
Supressing Input :
The scanf() function can be used to read a field without assigning it to any variable. This is done
by preceeding that fields format code with ‘ * ‘
For example:
Scanf ( “%d * c %d“, &hr, &min );
The time can be read as 9:05
Here the colon would be read but not assigned to anything. Therfore, assignment supression is
particulary useful when the part of what is input needs to be supressed.
Using Scanset :
Scanset is used to define a set of characters which may be read and assigned tot he corresponding
string.
Scanset is defined by placing the characters inside square brackets prefixed with a % as shown in
the example below
% [ “ aeiou “ ]
When we use the above scanset, scanf() will continue to read characters and store then into string
variable until it encounters a character that is not specified in the scanset.
Example:
#include <stdio.h>
int main ()
{
Char str [10];
Printf(“entert the string“);
Scanf(“%[aeiou]“, str);
Printf (“the string is %s“, str):
Return 0;
}
String Taxonomy:
We can store a string either in fixed-length format or in variable-length format
Fixed-length strings
When storing a string in a fixed length format, you need to specify an appropriate size for
the string variable. if the size is too small then you will not be able to store all the elements in the
string. On the other hand, if the string size is large, then unnecessarily memory space will be
wasted.
Variable-length strings
A beter option is to use a variable length format in which the string can be expanded or
contracted to accommodate the elements in it.
For example,
if you declare a string variable to store the name of a student. If a student has along name of say
20 characters, then the string can be expanded to accommodate 20 characters.
On the other hand, a student name has only 5 characters, then the string variable can be
contracted to store only 5characters. However, to use a variable-length string format you need a
technique to indicate the end of elements that are a part of the string. Thsi can be done either by
using length-controlled string or a delimiter.
Length-Controlled Strings:
In a length-controlled string you need to specify the number of characters in a string. This count
is used by the string functions to determine the actual length oft he string variable.
Delimited Strings:
In this format, the string is ended with a delimiter. The delimiter is then used to identify the end
oft he string. We can use any character such as comma, semicolon, dash, asterisk, null character,
etc.. as a delimiter of a string.
Operations On Strings:
1. Length of a string
2. Converting characters of a string into UpperCase
3. Converting characters of a string into LowerCase
4. Concatenating two strings to form a new string
5. Apending a string to another string
6. Comparing two strings
7. Reversing a string
8. Extracting a substring from left, right, middle.
9. Inserting a string in another string
10. Indexing
11. Deleting a string from the main string
12. Replacing a pattern with another pattern in a string.
• Length of a string :
Specifies the number of characters in a given string.
/* C program to find length of the string with out using strlen function */
#include<stdio.h>
#include<string.h>
void main()
{
char str1[50];
int i,count;
printf("Enter a string\n");
scanf("%s",str1);
count=0;
for(i=0;str1[i]!='\0';i++)
count=count+1;
#include<stdio.h>
#include<string.h>
void main()
{
char str1[20],str2[20],str3[50];
int i,k;
printf("Enter String1\n");
scanf("%s",str1);
printf("Enter String2\n");
scanf("%s",str2);
k=0;
for(i=0;str1[i]!='\0';i++)
str3[i]=str1[i];
k=k+1; }
for(i=0;str2[i]!='\0';i++) {
str3[k]=str2[i];
k=k+1; }
str3[k]='\0';
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
char str1[20],str2[20];
int len1,len2,i;
scanf("%s",str1);
scanf("%s",str2);
len1=strlen(str1);
len2=strlen(str2);
if(len1!=len2)
else
{
for(i=0;str1[i]!='\0';i++)
{
if(str1[i]!=str2[i])
{
printf("Strings are different\n");
exit(0);
#include<stdio.h>
#include<string.h>
void main()
{
char str1[50],str2[50];
int i,j,len;
printf("Enter String1\n");
gets(str1);
j=0; len=strlen(str1);
for(i=len-1;i>=0;i--)
str2[j]=str1[i];
j++;
str2[j]='\0';
Enter String1
Expectopetronum
The reversed string = munortepotcepxE
String Length :- The function strlen() is used to find the length of the string in terms of
number of characters in it.
SYNTAX: strlen(string_data);
#include<stdio.h>
#include<string.h>
void main()
{
char str1[50];
int len;
printf("Enter a string\n");
scanf("%s",str1);
len=strlen(str1);
printf("Length of the String=%d\n",len);
String Compare:- The function strcmp() is used to compare the string data every character of one string
is compared with the corresponding position character of second string.
SYNTAX strcmp(str1,str2)
#include<stdio.h>
#include<string.h>
void main()
{
char str1[20],str2[20];
int k;
printf("Enter string 1\n");
scanf("%s",str1);
scanf("%s",str2);
k=strcmp(str1,str2);
if(k==0)
printf("Strings are same\n");
else
printf("Strings are different\n");
String n Compare :-The strncmp() funtion compares the n characters of one string to another
string
#include<stdio.h>
Information Science and Engineering, JIT
Principles of Programming using C BPOPS203
#include<string.h>
void main()
{
char str1[20],str2[20];
int k,n;
printf("Enter string 1\n");
scanf("%s",str1);
scanf("%s",str2);
scanf("%d",&n);
k=strcmp(str1,str2,n);
if(k==0)
printf("Strings are same\n");
else
printf("Strings are different\n");
String Copy:- The function strcpy() copies the content from one string to another string
SYNTAX : strcpy(str2,str1);
#include<stdio.h>
#include<string.h>
void main()
{
char str1[30],str2[30];
printf("Enter string1\n");
scanf("%s",str1);
strcpy(str2,str1);
printf("The copied string is = %s\n",str2);
#include<stdio.h>
#include<string.h>
void main()
{
scanf("%s",str1);
while(str1[i]!='\0')
{
str2[i]=str1[i];
i++; }
str2[i]='\0';
printf("The Original String=%s\n",str1);
String n Copy :-The strncpy() funtion copies the n characters of one string to another string
SYNTAX : strncpy(dest_string,source_string,n);
Where n is an integer value which specifies the number of characters to be copied
#include<stdio.h>
#include<string.h>
void main()
{
char str1[30],str2[30];
printf("Enter String1\n");
gets(str1);
printf("String 1= %s\n",str1);
strncpy(str2,str1,10);
printf("String 2= %s\n",str2);
}
***** OUTPUT *****
String Concatenate :- The function strcat() is used to concatenate (attach) two strings.
SYNTAX : strcat(str1,str2);
#include<stdio.h>
#include<string.h>
void main()
{
char str1[30],str2[30];
printf("Enter String1\n");
scanf("%s",str1);
Information Science and Engineering, JIT
Principles of Programming using C BPOPS203
printf("Enter String2\n");
scanf("%s",str2);
strcat(str1,str2);
printf("The concatenated string is=%s\n",str1);
String n Concatenate: - The function strncat() is used to concatenate the specified number
of characters only
SYNTAX : strncat(string1,string2,n);
#include<stdio.h>
#include<string.h>
void main()
{
char str1[30],str2[30];
printf("Enter String1 and String2\n");
scanf("%s%s",str1,str2);
strncat(str1,str2,5);
printf("The concatenated string is=%s\n",str1);
strrev(str1)
#include<stdio.h>
#include<string.h>
void main()
{
char str1[30];
printf("Enter String1\n");
gets(str1);
strrev(str1);
printf("The Reversed string =%s\n",str1);
#include<stdio.h>
#include<string.h>
void main()
{
char str1[50],str2[50];
int i,j,len,x;
printf("Enter String1\n");
gets(str1);
j=0; len=strlen(str1);
for(i=len-1;i>=0;i--)
str2[j]=str1[i];
Information Science and Engineering, JIT
Principles of Programming using C BPOPS203
j++; }
str2[j]='\0';
printf("The original String =%s\n",str1);
x=strcmp(str1,str2);
if(x==0)
printf("%s is a palindrome\n",str1);
else
malayalam is a palindrome
char string_name[size1][size2];
where,
The two dimensional array of strings is an array of one dimensional character array which consist
of strings as its individual elements.
Syntax:
#include<stdio.h>
void main()
{
char str[50][50];
int n,i;
printf(“Enter the number of names\n”);
scanf(“%d”,&n);
printf(“Enter %d names\n”,n);
for(i=0;i<n;i++)
{
scanf(“%s”,str[i]
for(i=0;i<n;i++)
{
printf(“%s\n”,str[i]
Pointers:
Pointer: - A pointer is a variable which holds address of another variable or a memory-location.
Advantages of Pointers:
• Pointers are more efficient in handling arrays and data tables.
• Pointers can be used to return multiple values from a function.
• Pointers allow ‘C’ to support dynamic memory management.
• Pointers provide when efficient tool for manipulating dynamic data structures such as
int *ptr;
then
ptr = &a;
*ptr = a;
#include<stdio.h>
void main()
{
int *ptr;
int x=22;
printf("Addres of x= %d\n",&x);
printf("Content of x= %d\n",x);
ptr=&x;
printf("Addres of pointer= %d\n",ptr);
Types of Pointers:
1. Null Pointers:
We have seen that a pointer variable is a pointer to some other variable of the same
data type. However in some cases we may prefer to have null pointer which is a special
pointer that doesnot 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>,<string.h>.
int ptr;
ptr = 0;
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 be used to point to a variable of
any data type.
It is declared as a normal pointer variable but using the void keyword as the pointers data
type.
For example:
void *ptr;
Since you cannot have a variable of type void, the void pointer will therefore not point to
any data and thus cannot be dereferenced. You need to typecast a void pointer to another
kind of pointer before using it.