Académique Documents
Professionnel Documents
Culture Documents
In c-language strlen(), strcpy(), strcat(), strcmp() are some in-built functions. If we want to
use these functions we’ve to include the header file “string.h”. strlen() returns the length of
a string, strcat() concatenates two strings, strcpy() copies one string to other & strcmp()
compares two strings. In this program we write own functions for strlen(), strcpy(), strcat(),
strcmp().
S.N
Function & Purpose
.
strcpy(s1, s2);
1
Copies string s2 into string s1.
strcat(s1, s2);
2
Concatenates string s2 onto the end of string s1.
strlen(s1);
3
Returns the length of string s1.
strcmp(s1, s2);
4 Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if
s1>s2
ALGORITHM:
INPUT: Two string ‘str’ & ‘s’. For palindrome we may input a new string ‘str2’.
(v)Palindrome check & finding reverse of ‘str’, ‘s’ or another string ‘str2’.
PROCESS:
Step 1: [Creation of the function “int stlen(char str[30])” in which we pass the string “str[30]” & the functions
Initially i 1.
i i+1.
[end of while]
Step 2: [Creation of the function “void palindrome(char str[30])” in which a string ‘str[30]’ pass as argument &
Step 2.3: for i=0, j=stlen(str)-1 i.e. ((the length of the string ‘str’)-1) to i<j continue
step 2.4.
else
[end of step 2.3 for loop and ‘i’ will be post increment to ‘i+1’, ‘j’ will be post decrement to ‘j-1’.]
else
step 2.7.
c str[i]
str[i] str[j]
str[j] c
[end of step 2.6 for loop and ‘i’ will be post increment to ‘i+1’, ‘j’ will be post decrement to ‘j-1’.]
Display ‘str’.
Step 3: [Creation of the function “char *stringcat(char *str1,char *str2)” in which we pass two strings ‘str1’ & ‘str2’
as pointer, after concatenating two strings to ‘str 1’, it returns the address of ‘str1’. ]
Step 3.1: declare two integer type variables ‘i’ & ‘l’.
Step 3.3: i 0
Step 4: [creation of the function “char *stringcpy(char *str1,char *str2)” in which we pass two strings ‘str1’ & ‘str2’
as pointer, and it copies string ‘str2’ to ‘str1’ & returns the address of ‘str1’]
Initially i 0.
Step 4.2: repeat step 4.3 to 4.4 while while(str2[i] ≠ ‘\0’)
Step 5: [creation of the function “int stringcmp(char str1[30],char str2[30])” in which we pass the strings ‘str1[30]’
& ‘str2[30]’ & comparing these two strings it returns an integer value. ]
Step 5.2: repeat step 5.3 to 5.4 while (str1[i]≠ ‘\0’ && str2[i]≠ ‘\0’)
Return(str1[i] – str2[i])
Input str.
Input s.
2.string copy
3.string concatenation
4.string comparison
5.reverse string
6.exit
Input ch.
Case 1:
Display (l=stlen(str)).
Display (l=stlen(s)).
Case 2:
stringcpy(str,s)
Step b) print (" after copying the 2nd string to 1st string, the 1st
string is:”)
display(str).
Step c) break
Case 3:
Step a) [concatenating ‘str’ & ‘s’ & store the concatenated string
to str ]
stringcat(str,s)
Display (str)
Step c) Break.
Case 4:
Step a)[Comparing “str” & “s”, store the result in l & display]
Display (l=stringcmp(str,s))
Step b) Break.
Case 5:
Step a) print("enter 1 for first string,2 for 2nd string,3 for any other
string:")
input l.
if (l=1) then
palindrome(str)
else if (l=2)
palindrome(s)
i) else
iv) gets(str2)
v) palindrome(str2)
Step e) break.
Case 6:
Default:
[when any other number is input except 1 to 6]
Break.
Step 6.8: [end of step 6.6 do-while & repeat step 6.7 to 6.8 under the condition
while(1)]
PROGRAMMING CODE:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<process.h>
int i=1;
while(str[i]!='\0')
i++;
return(i);
int i,j;
char c;
for(i=0,j=(stlen(str))-1;i<j;i++,j--)
if(str[i]==str[j])
continue;
else
break;
if(i>=j)
else
for(i=0,j=(stlen(str))-1;i<j;i++,j--)
c=str[i];
str[i]=str[j];
str[j]=c;
puts(str);
int i,l;
l=stlen(str1);
i=0;
while(str2[i]!='\0')
str1[l]=str2[i];
l=l+1;
i=i+1;
str1[l]='\0';
return(str1);
}//end of function
int i=0;
while(str2[i]!='\0')
str1[i]=str2[i];
i++;
str1[i]='\0';
return(str1);
int i=0;
if(str1[i]!=str2[i])
return(str1[i]-str2[i]);
i++;
return(str1[i]-str2[i]);
void main()
int ch,l;
char str[30],s[30],str2[30];
gets(str);
gets(s);
do
scanf("%d",&ch);
switch(ch)
case 1:
break;
case 2:
stringcpy(str,s);
printf("\n after copying the 2nd string to 1st string,\nthe 1st string is:");
puts(str);
break;
case 3:
stringcat(str,s);
puts(str);
break;
case 4:
printf("string comparison=%d\n",l=stringcmp(str,s));
break;
case 5:
printf("\nenter 1 for first string,2 for 2nd string,3 for any other string:");
scanf("%d",&l);
if(l==1)
palindrome(str);
else if(l==2)
palindrome(s);
else
{
fflush(stdin);
gets(str2);
palindrome(str2);
break;
case 6:
exit(0);
break;
default:
break;
}while(1);
getch();
OUTPUT:
DISCUSSION: It is comprised of a set of characters that can also contain spaces and numbers. For
example, the word "hamburger" and the phrase "I ate 3 hamburgers" are both strings. Even
enclose strings in quotation marks for the data to recognized as a string and not a number or
variable name.