You are on page 1of 17

Strings

In C an array of characters is known as a String.


In C language 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.
char str[ ]="Hello";
H e l l o \0

char str[ ]="H";


H \0
When allocating memory space for a character array, reserve
space to hold the null character also.

H
e
l
l
o
\0
Memory representation of a character array.
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, like
char str[ ]={'H', 'e' , 'l' , 'l' , 'o' , '\0' };
char str[10] ="Hello";
Reading Strings :-
If we declare a string by writing
char str[100];
then str can be read by using following statements:
(i)using scanf( ) function
(ii)using gets( ) function
/* Program using the gets( ) function */
#include<stdio.h>
#include<conio.h>
void main()
{
char name[20];
clrscr();
printf("Enter the name\n");
gets(name);
printf("\n The entered string is %s\n",name);
getch();
}
O/P:-
Enter the name
Chennai
The entered string is Chennai

C – Read String using Scanf()

To read a string entered by user via standard input in C language,


use scanf() function.

#include <stdio.h>
#include<conio.h>
void main() {
char name[30];
clrscr();
printf("Enter your name : ");
//read input from user to "name" variable
scanf("%s", name);
printf("Hello %s!\n", name);
getch();
}
O/P:-
Enter your name: RAMA
Hello RAMA!

Writing strings:-
Strings can be displayed on screen using using following
statements:
(i) using printf ( ) function
(ii) using puts( ) function

A string can be displayed using printf( ) by writing


printf("%s",str);

puts(str);

Write a program to display a string using printf( ):-


#include<stdio.h>
#include<conio.h>
void main()
{
char str[]="Introduction to C";
clrscr();
printf("\n%s",str);
getch();
}
o/p:-
Introduction to C

program for puts( )

#include<stdio.h>
#include<conio.h>
void main()
{
char name[20];
clrscr();
puts("Enter the name");
gets(name);
puts("\n the entered string is ");
puts(name);
getch();
}
O/P:-

Enter the name


Tamil Nadu
the entered string is
Tamil Nadu

String Handling Functions


The C string functions are built-in functions that can be used
for various operations and manipulations on strings. These
string functions can be used to perform tasks such as string
copy, concatenation, comparison, length, etc. The <string.h>
header file contains these string functions.

Functi
on Purpose Syntax
Calculates the length of a character string
strlen str1. strlen(str1);
strcmp(str1,
strcmp Compares two strings. str2);
concats or joins first string with second strcat(str1,
strcat string. str2);

Copies the source string src(source) to strcpy(str1,str


strcpy the destination string 2);
Dest(destination).
strrev Reverse the content of the string s. strrev(str1);
strlwr Converts the string to lowercase. strlwr(str1);
strupr Converts the string to uppercase. strupr(str1);

Programs using predefined function

To find the length of


the string
#include <stdio.h>
#include <string.h>
#include<conio.h>
void main()
{
char a[50];

int l;

clrscr();
printf("Enter a string\n");
scanf("%s",a);
l = strlen(a);
printf("Length of a string is
%d\n",l);
getch();
}

Output:
Enter a
string
Hai
Length of a string is 3
Strcat()
String concatenation:-
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[]="happy",str2[]="welcome";
clrscr();
//concatenates str1 and str2 and resultant string is stored in
str1.
strcat(str1,str2);
puts(str1);
puts(str2);
getch();
}
o/p
happywelcome
welcome
String copy:- strcpy()
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[10]="awesome";
char str2[10];
char str3[10];
clrscr();
strcpy(str2,str1);
strcpy(str3,"well");
puts(str2);
puts(str3);
getch();
}
o/p
awesome
well
string Reverse :-strrev( )
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char arr[100];
clrscr();
printf("\n Enter a string to reverse\n");
gets(arr);
strrev(arr);
printf("Reverse of entered string is \n%s\n",arr);
getch();
}
o/p
Enter a string to reverse
RAMANA
Reverse of entered string is
ANAMAR
Strcmp( ):-
The strcmp function compares the two strings character by
character using their ASCII values and returns any one of the
following integer values.
Return value
Less than 0 - ASCII value of the character of the first string is less
than the ASCII value of the corresponding character of the second
string.
Greater than 0 - ASCII value of the character of the first string is
greater than the ASCII value of the corresponding character of the
second string.
Equal to 0 –If the strings are identical.
The ASCII value of the lowercase alphabet is from 97 to 122. And,
the ASCII value of the uppercase alphabet is from 65 to 90. If the
ASCII value of the character entered by the user lies in the range
of 97 to 122 or from 65 to 90, that number is an alphabet.
Program to compare two strings:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[20],str2[20];
int result;
clrscr();
puts("Enter the string1");
gets(str1);
puts("Enter the string2");
gets(str2);
result=strcmp(str1,str2);
if(result==0)
puts("Strings are Equal");
else
puts("Strings are not Equal");
getch();
}
o/p:-
Enter the string1
Ram
Enter the string2
Ram
Strings are Equal
Strlwr() function
strlwr( ) function converts all the letters in a string to lowercase.
When we are passing any upper case string to this function it
converts it into lowercase.
Program using strlwr( ) function:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[20];
clrscr();
puts("Enter the string");
gets(str);
strlwr(str);
puts("lower case string is ");
puts(str);
getch();
}
o/p:-
Enter the string
RAM
lower case string is
ram
strupr() function:-
strupr() function converts all the letters in a string to upper case.
Program using strupr( ) function:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[20];
clrscr();
puts("Enter the string");
gets(str);
strupr(str);
puts("Upper case string is ");
puts(str);
getch();
}
o/p:-
Enter the string
ram
Upper case string is
RAM
Write a C program for Palindrome of a given string.
#include <stdio.h>
#include <string.h>
#include<conio.h>
void main() {
char inputArray[100], reversedArray[100];
clrscr();
printf("Enter the string\n");
scanf("%s", inputArray);
/* Copy input string and reverse it*/
strcpy(reversedArray, inputArray);
/* reverse string */
strrev(reversedArray);
/* Compare reversed string with inpit string */
if(strcmp(inputArray, reversedArray) == 0 )
printf("%s is a palindrome.\n", inputArray);
else
printf("%s is not a palindrome.\n", inputArray);
getch();
}
Output:
Enter the string
MADAM
MADAM is a palindrome.
Linear Search
Linear search is a sequential searching algorithm where we start
from one end and check every element of the list until the desired
element is found. It is the simplest searching algorithm.
How Linear Search Works?
The following steps are followed to search for an element k = 1 in
the list below.

Array to be searched for


Compare with each element
If x == k, return the index

Element found
else, return not found.
C program for linear search
#include <stdio.h>
#include<conio.h>
void main()
{
int array[100], search, c, n,count=0;
clrscr();
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d integer(s)\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter a number to search\n");
scanf("%d", &search);
for (c = 0; c < n; c++)
{
if (array[c] == search) /* If required element is found */
{
printf("%d is present at location %d.\n", search, c+1);
count++;
break;
}
}
if(count==0)
printf("%d is not found",search);
getch();
}

You might also like