String Exp1
String Exp1
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‟.
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.
string
1
Data Structures and Algorithm Lab SE (ECE)
1. Substring
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
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
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
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
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);
}
}
printf("\n String is a palindrome");
flag = 1;
break;
}
i++;
}
if (flag ==0)
return 0;
else
return 1;
}
6
Data Structures and Algorithm Lab SE (ECE)
7
Data Structures and Algorithm Lab SE (ECE)
/* OUTPUT
V=Enter the string 1:Hello world
lo wor
8
Data Structures and Algorithm Lab SE (ECE)
--------------------------------
Process exited after 77.98 seconds with return value 0
Press any key to continue . . .
*/