Strings
Strings
Strings
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
#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
puts(str);
#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:-
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);
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.
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();
}