STRINGS
1-D character Arrays
Like an integer array, a group of characters can be
stored in a character array. These character arrays are often called as
strings. A string is an array of characters. A string is a nothing but a
collection of characters.
The string science would be stored as
S
|
C
|
I
|
E
|
N
|
C
|
E
|
\0
|
\0 – Null indicates the end of the string.
Defining
a 1-D character Array:
Char
st[20];
The above statement tells the compiler to reserve
sufficient amount of space to store 20 characters and specify a name st to the
entire collection.
Initialization
of 1-D character array:
Char st[]={‘v’,’I’,’k’,’r’,’a’,’m’};
Or
Char st[]=”vikram”;
Reading/printing
a group of characters
#include<stdio.h>
void main()
{
char st[30];
clrscr();
printf("Enter the name:");
scanf("%s”, st);
printf("The entered name is:%s
“,st)
}
Output
Enter a
name: Mahesh Babu
Babu
The %s format specifier in the scanf() keeps on
reading the characters until it encounters a whitespace character(space/tab
spcae/enter key) and place a special character called as null character at the
end of the string. The %s format in scanf( ) can accept only single word. So
the above output. gets( ) is an exclusive function for reading strings. Puts( )
is a function which prints the given string.
#include<stdio.h>
void main(
)
{
char na[30];
printf("Enter
the name:");
gets(na);
printf("%s",na);
}
Output
Enter a
name: Mahesh Babu
Mahesh Babu
The common
operations performed on character strings are:
- Reading & Printing
Strings
- Combining 2 strings
together
- Copying 1 string into
another
- Comparing 2 strings etc.
We can
perform these operations through string functions which are defined by c and
defined in <string.h> header file
String Handling Using String Library
Functions
Some of the
predefined string functions are:
1. strlen ( ) : this function is used to find
out the length of the string.
Syntax: strlen(var_name);
Ex: char
name="mahesh"
strlen(name); //it returns 6
//program for finding the string
length
#include<string.h>
#include<stdio.h>
void main(
)
{
char na[30];
int l;
printf("Enter
the name:");
gets(name);
l=strlen(name);
printf("Length
of the string is ...%d",l);
}
Output
Enter the
name: gayatri
Length of
the string is…7
2. strcpy ( ) : this function is used to copy
string from one into another string.
Syntax: strcpy(target_string,source_string);
Ex: strcpy(str2,str1);
//program for copying one string into
the other string
#include<stdio.h>
#include<string.h>
void main(
)
{
char
source[30],target[30];
printf("Enter
the name:");
gets(source);
strcpy(target,source);
printf("Source
String is...%s\n",source);
printf("Target
String is...%s\n",target);
}
Output
Enter the
name: gayatri
Source
String is…gayatri
Target
String is…gayatri
3. strrev ( ) : this function is used to
reverse the given string.
Syntax:
strrev(string);
ex:
strrev(str1);
//program for reversing string
#include<stdio.h>
#include<string.h>
void main(
)
{
char
source[30];
printf("Enter
the name:");
gets(source);
strrev(source);
printf("Reverse
String is...%s\n",source);
}
Output
Enter the
name: gayatri
Reverse
String is...irtayag
4. strcmp ( ) : this function is used to compare
the given two string.
Syntax:
strcmp(str1,str2);
ex:
strcmp(str1,str2);
//program for comparing string
#include<stdio.h>
#include<string.h>
void main(
)
{
char
source[30],dest[30];
int n;
printf("Enter
the source name:");
gets(source);
printf("\nEnter
the destination name:");
gets(dest);
n=strcmp(source,dest); //if it is same it returns 0
otherwise -1
if(n==0)
printf("The
given 2 strings are same");
else
printf("The
given 2 strings are not same");
}
Output
Enter the
source name: gayatri
Enter the
destination name: gayatri
The given 2
strings are same
Enter the
source name: gayatri
Enter the
destination name: vidya
The given 2
strings are not same
5. strcat ( ) : this function is used to
combine the given two string.
Syntax:
strcat(str1,str2);
Ex: strcat(str1,str2);
//program for concatenating 2 strings
#include<stdio.h>
#include<string.h>
void main(
)
{
char
first[30],second[30],combine[30];
printf("Enter
the first name:");
gets(first);
printf("\nEnter
the second name:");
gets(second);
strcpy(combine,first); // first copy first name
into
combine
strcat(combine,second); //now add second
name
to combine
printf("First
Name:%s",first);
printf("Second
Name:%s",second);
printf("Combined
Name:%s",combine);
}
Output
Enter the first
name: gayatri
Enter the
second name: vidya
Combined
Name: gayatrividya
6. strlwr( ): it converts given string into
lowercase
Syntax:
strlwr("GVP");
7. strupr( ):
it converts given string into uppercase
Syntax:
strupr("gvp");
A palindrome is nothing but when a name is given if
its reversed the reversed name should be same to the given number. Ex: Liril,
Malayalam
/* String Palindrome using string
functions */
#include<stdio.h>
#include<string.h>
main()
{
char
str[50],str1[50];
int n;
clrscr();
printf("Enter
the string:");
gets(str);
strcpy(str1,str); //string copying
strrev(str1); //string reversing
n=strcmp(str1,str); //string comparison
if(n==0)
printf("\ngiven
string is palindrome");
else
printf("\ngiven
string is not palindrome");
getch();
}
Output:
Enter the
string: liril
given
string is palindrome
/* String Palindrome without using
string functions */
#include<stdio.h>
#include<string.h>
main()
{
char str[50],str1[50];
int flag=0,i,l=0,j;
clrscr();
printf("Enter the string:");
gets(str);
for(i=0;str[i]!='\0';i++)
l++; /* length of the given string */
for(i=0,j=l-1;j>=0;i++,j--)
str1[j]=str[i]; /* reversing the string */
for(i=0;i<l;i++)
{
if(str1[i]==str[i]) /* str contains the given string
*/
continue; /*str1
contains the reverse string */
else /* comparing 2 strings */
flag=1;
}
if(flag==0)
printf("\ngiven string
is palindrome");
else
printf("\ngiven string
is not palindrome");
getch();
}