Vous êtes sur la page 1sur 14

ASSIGNMENT:13

SUBSTUTION fUNCTION FOR strlen(), strcpy(),


strcat(), & strcmp() FOR STRING AND WRITE YOUR
OWN FUNCTION FOR REVERSE OF A STRING
The string in C programming language is actually a one-dimensional array of characters
which is terminated by a null character '\0'. Thus a null-terminated string contains the
characters that comprise the string followed by a null.

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’.

OUTPUT:(i)Length of the string ‘str’ & ‘s’.


(ii)Copying a string to other

(iii)Concatenating two string ‘str’ & ‘s’.


(iv)Comparison of two string ‘str’ & ‘s’.

(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

returns an integer value i.e. length of the string.]

Step 1.1: Declare an integer type variable ‘i’.

Initially i  1.

Step 1.2: Repeat while (str[i] ≠ ‘\0’)

i  i+1.

[end of while]

Step 1.3: Return i.

[end of “int stlen(char str[30])” function.]

Step 2: [Creation of the function “void palindrome(char str[30])” in which a string ‘str[30]’ pass as argument &

return type is ‘void’.]

Step 2.1: Declare integer type variable ‘i’ & ‘j’.

Step 2.2: Declare character type variable ‘c’.

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.

Step 2.4: if (str[i] = str[j])

Continue the loop.

else

Exit from loop by using ‘break’ keyword.

[end of step 2.3 for loop and ‘i’ will be post increment to ‘i+1’, ‘j’ will be post decrement to ‘j-1’.]

Step 2.5: if(i ≥ j)

Print(“given string palindrome”).

else

Print (“given string isn’t palindrome”).


Step 2.6: for i=0, j=stlen(str)-1 i.e. ((the length of the string ‘str’)-1) to i<j continue

step 2.7.

Step 2.7: [Swapping technique]

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’.]

Step 2.8: print (“reverse of the string is:”)

Display ‘str’.

[end of the function “void palindrome(char str[30])”]

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.2: l  stlen(str 1) i.e. length of ‘str 1’.

Step 3.3: i  0

Step 3.4: repeat step 3.5 to 3.7 while (str2[i] ≠ ‘\0’)

Step 3.5: str1[l]  str2[i]

Step 3.6: l  l+1

Step 3.7: i  i+1

[end of step 3.4 while loop.]

Step 3.8: str1[l]  ‘\0’

Step 3.9: return the address of ‘str1’.

[end of “char *stringcat(char *str1,char *str2)” function]

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’]

Step 4.1: declare an integer type variable ‘i’ &

Initially i  0.
Step 4.2: repeat step 4.3 to 4.4 while while(str2[i] ≠ ‘\0’)

Step 4.3: str1[i]  str2[i]

Step 4.4: ii+1

[end of step 4.2 while loop]

Step 4.5: str1[i]  ‘\0’

Step 4.6: return the address of ‘str1’.

[end of “char *stringcpy(char *str1,char *str2)” function.]

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.1: Declare an integer variable ‘i’ & initially ‘i’  0.

Step 5.2: repeat step 5.3 to 5.4 while (str1[i]≠ ‘\0’ && str2[i]≠ ‘\0’)

Step 5.3: if (str1[i] ≠ str2[i])

Return(str1[i] – str2[i])

Step 5.4: ii+1

[end of step 5.2 while loop]

Step 5.5: return(str1[i] – str2[i])

[end of “int stringcmp(char str1[30],char str2[30])” function.]

Step 6: [Creation of main() function]

Step 6.1: Declare integer type variable ch, l.

Step 6.2: Declare three strings str[30], s[30], str2[30].

Step 6.3: print (“enter the first string:”)

Input str.

Step 6.4: print (“enter the second string:”)

Input s.

Step 6.5: [starting of do-while loop]

Step 6.6: print ("1.length of string

2.string copy
3.string concatenation

4.string comparison

5.reverse string

6.exit

enter your choice:");

Input ch.

Step 6.7: switch case construction under the value of ‘ch’

Case 1:

Step a) print (“length of first string=”)

Display (l=stlen(str)).

Step b) print (“length of second string=”)

Display (l=stlen(s)).

Step c) “break” keyword is used to break from the case.

Case 2:

Step a) [copying string ‘s’ to ‘str’]

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)

Step b) print (“after concatanating the string is:")

Display (str)
Step c) Break.

Case 4:

Step a)[Comparing “str” & “s”, store the result in l & display]

Print ("string comparison=")

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.

Step b) [finding palindrome of first string when ‘l’=1]

if (l=1) then

palindrome(str)

Step c) [finding palindrome of second string when ‘l’=2]

else if (l=2)

palindrome(s)

Step d) [otherwise taking input a new string “str2”, we find it’s

Palindrome, before store the string we must clear the buffer.]

i) else

ii) printf("Enter your string:")

iii) fflush(stdin) used to clear buffer.

iv) gets(str2)

v) palindrome(str2)

Step e) break.

Case 6:

“Exit 0” to exit from main() function.

Default:
[when any other number is input except 1 to 6]

Print(“check & give proper option!!!”)

Break.

Step 6.8: [end of step 6.6 do-while & repeat step 6.7 to 6.8 under the condition

while(1)]

Step 6.9: stop

[end of main() function]

PROGRAMMING CODE:
#include<stdio.h>

#include<conio.h>

#include<string.h>

#include<process.h>

/*function for substitution strlen()*/

int stlen(char str[30])//prototype declaration

int i=1;

while(str[i]!='\0')

i++;

return(i);

}//end of the function

/*function to find reverse of a string & check it is palindrone or not*/

void palindrome(char str[30])//prototype declaration


{

int i,j;

char c;

/*operation to check the string palindrome or not*/

for(i=0,j=(stlen(str))-1;i<j;i++,j--)

if(str[i]==str[j])

continue;

else

break;

if(i>=j)

printf("\ngiven string is palindrome!!!\n");

else

printf("\ngiven string isn't palindrome!!!\n");

/*operation to find reverse of a string*/

for(i=0,j=(stlen(str))-1;i<j;i++,j--)

c=str[i];

str[i]=str[j];

str[j]=c;

printf("\nreverse of the string is:");

puts(str);

}//end of the function

/*function to substitute strcat()*/


char *stringcat(char *str1,char *str2)//prototype declaration

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

/*substitution function for strcat()*/

char *stringcpy(char *str1,char *str2)//prototype declaration

int i=0;

while(str2[i]!='\0')

str1[i]=str2[i];

i++;

str1[i]='\0';

return(str1);

}//end of the function


/*substitution function for strcmp()*/

int stringcmp(char str1[30],char str2[30])//prototype declaration

int i=0;

while(str1[i]!='\0' && str2[i]!='\0')

if(str1[i]!=str2[i])

return(str1[i]-str2[i]);

i++;

return(str1[i]-str2[i]);

}//end of the function

void main()

int ch,l;

char str[30],s[30],str2[30];

printf("enter the first string:");

gets(str);

printf("enter the second string:");

gets(s);

do

printf("\n1.length of string\n2.string copy\n3.string concatanation\n4.string comparison\n5.reverse


string\n6.exit\nenter your choice:");

scanf("%d",&ch);
switch(ch)

case 1:

printf("\nLength of first string=%d",l=stlen(str));

printf("\nLength of second string=%d\n",l=stlen(s));

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);

printf("\n after concatanating the string is:");

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
{

printf("Enter your string:");

fflush(stdin);

gets(str2);

palindrome(str2);

break;

case 6:

exit(0);

break;

default:

printf("\ncheck & give proper option!!!");

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

"12345" could be considered a string, if specified correctly. Typically, programmers must

enclose strings in quotation marks for the data to recognized as a string and not a number or

variable name.

Vous aimerez peut-être aussi