Strings

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

STRINGS

Introduction

In C language a group of characters, digits and symbols enclosed within double


quotes are called as a String. The string always declared as a character array.
In other words, the character array is called as a String. To manipulate words and
sentences normally strings are preferable. Every string is terminated with a null character.
Functions gets() and puts() are two string functions to take string input from the user and display it
respectively. These functions are defined in ‘stdio.h’ header file.
Syntax to declare a String
char_data_type array_name[size];
Example
char ch[10];
In the above example the character array “ch” is able to store 9 characters and the last
character is a null character (‘\0’).
We can directly initialize the string variables as follows.
char ch[]={‘H’,’E’,’L’,’L’,’O’};
char ch[]=”Hello”;
char ch[]={{‘H’},{’E’},{’L’},{’L’},{’O’}};
Each character of the string occupies one byte of memory space. The last character is
always a null character (‘\0’). It is not compulsory to write a null character in the string. The
compiler automatically puts the null character at the end of the string.
The characters of the string are stored in continues memory locations as follows.

String Handling Functions or String Library Functions


The C-language provides the number of functions used to perform the manipulations
on strings and these functions are available in the header file <string.h>.
These are explained below.
1. strlen()
This function is used to find out the length of the string.
Syntax: strlen(string_name)
Example: strlen(ch);
The following program illustrate the use of strlen() function.
Aim: C-Program to illustrate strlen() function.
Program: /*strlen.c*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char ch[20];
int len;
clrscr();
printf("\n Enter a string:");
gets(ch); //func to read string from the user
/* finding the length of the string */
len=strlen(ch);
printf("\n The length of the given string is: %d",len);
getch();
}
Output:
2. strcmp()
It is used to compare two strings. It returns 0((zero) if two strings are equal
and it returns 1 if these are not equal.
Syntax: strcmp((string1,string2);
3. strcpy()
This function is used to copy the string2 into the string1 by deleting the
string2 data. Here the size of the string1 should be greater than the size of the string2.
Syntax: strcpy(string2,string1);

4. strlwr()
This function is used to convert the given string into the lower case string.
Syntax: strlwr(string);
Example: strlwr(“Hello World”);
Output: hello world
5. strupr()
This function is used to convert the given string into the upper case string.
Syntax: strupr(string);
Example: strupr(hello world”)
Output: HELLO WORLD

6. strcat()
This function is used to concatenate (appending the second string at the end of
the first string) two given strings.
Syntax: strcat(string1,string2);
Example: strcat(“Hello” “World”);
Output: HelloWorld
The following program illustrate the use of strcat() function.
Aim: C-program to demonstrate the use of strcat() function.

Program: /*strcat.c*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[10],s2[10];
clrscr();
printf("\n Enter the first string:");
gets(s1);
printf("\n Enter the second string:");
gets(s2);
/* Performing concatenation*/
strcat(s1,s2);
printf("\n The result string is: %s",s1);
getch();
}
Output:
7. strchr()
This function determines the first occurrence of the given character with in the
given string.
Syntax: strchr(string, character)
Example: strchr(“abcbcc”,’b’);
Output: 1
Programs
1. Write a C-Program to check whether the given string is palindrome or not.
Aim: C-Program to check whether the given string is palindrome or not.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[10],s2[10];
clrscr();
printf("\n Enter a string:");
gets(s1);

strcpy(s2,s1);
strrev(s1);
if(strcmp(s1,s2)==0)
printf("\n The given string is palindrome");
else
printf("\n The given string is not a palindrome");
getch();
}
Output:

Example Program to check the given character is uppercase or lower case

#include <stdio.h>

int main()
{

char ch;
printf("Enter the alphabet:");

scanf("%c",&ch);
if(ch>=65 && ch<=90)

printf("upper case");

else if(ch>=97 && ch<=122)

printf("lower case");

else
printf("Invalid input");
return 0;
}
Output
Enter the alphabet:g
lower case

Array of strings or Strings of arrays


A string is character array. If we want to refer arrays in strings we have to go for two
dimensional character arrays, because strings are nothing but a character array. So to
implement the string with arrays we have to take another indices [ ].
Syntax for declaring the array of strings:
char string_name[no.of strings][ size of each string];
Example: char book[2][10];

The above represents book is a string variable capable of storing two strings and each
string size is 10.
The following program will explain how to use array of strings in C-Language.
Aim: C-Program to demonstrate array of strings.
Program: /*strarray.c*/
#include<stdio.h>
#include<conio.h>
void main()
{
char book[20][20];
int n,i;
clrscr();
printf("\n Enter the number of books:");
scanf("%d",&n);
printf("\n Enter the books tittles\n");
for(i=0;i<=n-1;i++)
{
scanf("%s",book[i]);
}
printf("\n The book tittles are....\n");
for(i=0;i<n;i++)
{
puts(book[i]);
}
getch();
}
Output:
Important Questions
1. Define string and explain about declaration and initialization of strings in C.
2. Define string and explain about string handling functions in C with suitable examples.
3. Define string and explain about character array in C with example program.

You might also like