Data Structures and Algorithm Lab SE (ECE)
Experiment No. 1
Perform following String operations with and without pointers to arrays (without
using the library functions):
a. substring
b. palindrome
c. compare
d. copy
e. reverse
Theory:
Strings are defined as an array of characters. The difference between a character array and a string
is the string is terminated with a special character „\0‟.
Declaration of strings: Declaring a string is as simple as declaring a one dimensional array.
Below is the basic syntax for declaring a string.
char str_name[size];
In the above syntax str_name is any name given to the string variable and size is used define the
length of the string, i.e the number of characters strings will store. Please keep in mind that there is
an extra terminating character which is the Null character („\0‟) used to indicate termination of
string which differs strings from normal character arrays.
Initializing a String: A string can be initialized in different
1. char str[] = "GeeksforGeeks";
2. char str[50] = "GeeksforGeeks";
3. char str[] = {'G','e','e','k','s','f','o','r','G','e','e','k','s','\0'};
4. char str[14] = {'G','e','e','k','s','f','o','r','G','e','e','k','s','\0'};
The more commonly-used string functions
The most commonly used functions in the string library are:
1. strcat - concatenate two strings
2. strcmp - compare two strings
3. strcpy - copy a string
4. strlen - get string length
5. strncat - concatenate one string with part of another
6. strncmp - compare parts of two strings
7. strncpy - copy part of a
string
1
Data Structures and Algorithm Lab SE (ECE)
1. Substring
Example string is MODERN
ERN is substring of string MODERN
Algorithm:
1. start
2. Declare char array a[30],b[30]
3. Accept the two string using scanf(“%s”,b);
4. check substring string using
int i,j,k; i=0;
while(a[i]!='\0'&&a[i]!=b[0]) i++;
j=0;
while(b[j]!='\0'&&a[i]==b[j] )
i++;
j++;
}
if(j==strlen(b))
printf("%s is substring of s",b,a);
else
printf("%s is not substring of %s",b,a);
2. String Copy
Algorithm:
1. start
2. Declare char array a[30],b[30]
3. Accept the string using scanf(“%s”,b);
4. copy string using int i=0;
while(b[i]!='\0')
{
a[i]=b[i]; i++;
} a[i]='\0';
5. print both strings
2
Data Structures and Algorithm Lab SE (ECE)
3. String Compare
Example:
If s1:abcde s2:abc strcmp gives result s1>s2 If
s1:abcd s2:abcf strcmp gives result s1<s2
If s1:abc s2:abc strcmp gives result s1==s2
Algorithm:
1. Start
2. Declare char array a[30],b[30]
3. Accept the string using scanf(“%s”,b);
4. Compare string using
int i; i=0;
while(a[i]!='\0'&& a[i]==b[i]) i++;
printf("i=%d",i);
if(a[i]>b[i])
printf("\ns1>s2"); else if
(a[i]<b[i]) printf("\ns1>s2");
else printf("\ns1=s2");
4. Palindrome
Example: If s1:abba here reverse string of s1 is exactly same as the original string so given
string is called as palindrome
Algorithm:
1. Start
2. Declare char array a[30],b[30]
3. Accept the two string using scanf(“%s”,b);
4. Check substring string using
int i,j,k; i=0;
k=strlen(a); j=k-1;
while(a[j]==a[i]&&a[i]!='\0')
{
i++;
j--;
}
if(i==k)
printf("\n%s is palindrom",a); else
printf("\n%s is not palindrom",a);
5. Stop
3
Data Structures and Algorithm Lab SE (ECE)
6. String Reverse
Algorithm:
1. Start
2. Declare an array char rev[100] to store the reversed string
3. Declare variables begin=0,end,i.
4. Calculate end = strlen(str1)-1.
for(i=0;str1[i]!='\0';i++)
{
rev[i]=str1[end];
end--;
}
5. Print the reversed string.
6. Stop.
Conclusion:
4
Data Structures and Algorithm Lab SE (ECE)
Program:
#include<stdio.h>
#include<stdlib.h>
void substring(char[],int,int);
void stringcopy(char []);
void palindrome(char[]);
int compare(char[],char[]);
void stringreverse(char [ ]);
void main()
{
int n;
char str1[100],str2[20];
int length,position,p;
printf("Enter the string 1:");
gets(str1);
printf("\nEnter string 2:");
gets(str2);
printf("\n Entered string is: ");
puts(str1);
printf("\n Entered string is: ");
puts(str2);
do
{
printf(" \n Enter your choice:");
printf("\n 1 for substring \n 2 for palindrome \n 3 for compare \n 4
for copy \n 5 for reverse \n 6 for exit: ");
scanf("%d",&n);
switch(n)
{
case 1:
printf("Enter the lenght of substring:");
scanf("%d",&length);
printf("Enter the position in substring:");
scanf("%d",&position);
substring(str1,position,length);
break;
case 2:
palindrome(str1);
break;
case 3:
p = compare(str1,str2);
if(p==0)
{
printf("Strings are same");
}
else
{
printf(" \nStrings are not same");
}
break;
case 4:
stringcopy(str1);
break;
case 5:
5
Data Structures and Algorithm Lab SE (ECE)
stringreverse(str1);
break;
case 6:
exit(0);
}
}while(n!=6);
}
void substring(char str1[],int position,int length)
{
char sub_str[50];
int i=0;
while(i<=length )
{
sub_str[i] = str1[position+i];
i++;
}
sub_str[i]='\0';
printf("\n\n");
puts(sub_str);
}
void palindrome(char str1[])
{
int start=0,end;
end = strlen(str1)-1;
while(end>start)
{
if(str1[start]!=str1[end])
{
printf("String is not palindrome");
return;
}
}
printf("\n String is a palindrome");
int compare(char str1[],char str2[])
{
int i=0,flag = 0;
while(str1[i]!='\0'&&str2[i]!='\0')
{
if (str1[i]!=str2[i])
{
flag = 1;
break;
}
i++;
}
if (flag ==0)
return 0;
else
return 1;
}
6
Data Structures and Algorithm Lab SE (ECE)
void stringcopy(char str1[])
{
char s2[20];
int i=0;
while(str1[i]!='\0')
{
s2[i]=str1[i];
i++;
}
printf("\n copied string is:");
puts(s2);
}
void stringreverse(char str1[])
{
char rev[100];
int begin=0,end,i;
end = strlen(str1)-1;
for(i=0;str1[i]!='\0';i++)
{
rev[i]=str1[end];
end--;
}
puts(rev);
}
7
Data Structures and Algorithm Lab SE (ECE)
Output (Screen Shots not allowed):
/* OUTPUT
V=Enter the string 1:Hello world
Enter string 2:My country is india
Entered string is: Hello world
Entered string is: My country is india
Enter your choice:
1 for substring
2 for palindrome
3 for compare
4 for copy
5 for reverse
6 for exit: 1
Enter the lenght of substring:5
Enter the position in substring:3
lo wor
Enter your choice:
1 for substring
2 for palindrome
3 for compare
4 for copy
5 for reverse
6 for exit: 2
String is not palindrome
Enter your choice:
1 for substring
2 for palindrome
3 for compare
4 for copy
5 for reverse
6 for exit: 3
Strings are not same
Enter your choice:
1 for substring
2 for palindrome
3 for compare
4 for copy
5 for reverse
6 for exit: 4
8
Data Structures and Algorithm Lab SE (ECE)
copied string is:Hello world
Enter your choice:
1 for substring
2 for palindrome
3 for compare
4 for copy
5 for reverse
6 for exit: 5
dlrow olleHD°
Enter your choice:
1 for substring
2 for palindrome
3 for compare
4 for copy
5 for reverse
6 for exit: 6
--------------------------------
Process exited after 77.98 seconds with return value 0
Press any key to continue . . .
*/