The program has six sub-tasks. Following operations on strings are performed: 1) Calculate the length of the string. 2) Copy the inputed string. 3) Concatenate two strings. 4) Reverse a string. 5) Compare two strings. 6) Check for sub string. The program has 6 functions, each performing one of the above operations. Data type used: The data type used is char Array.
'char' data type is used as it helps us to traverse every character in a string. Functions: 1) Length The first function is for calculating length of the string. eg: string = 'hello' length = 5 It takes string as an input parameter and returns the length of the string which is an integer value. The entire character array is traversed untill the it reaches to NULL (\0). For each character in the array the variable length is incremented. eg: For string 'hello' , variable length will be incremented 5 times (5 characters). Thus we get length of the string as 5. 2)Copy The entire string in character array 'A' is copied into character array 'B'. Each character in array A is individually traversed and copied into array B. eg: If string A = 'hello' , after copying string B = 'hello' . 3) Compare If the characters in array A are all same as the characters in array B then output will be "equal" else if they are not same, it will be "not equal". First, the length of of both the strings is checked. If the lengths are same then we proceed to check the individual characters. If the lengths are not same then the strings will not be equal. We check the individual elements and if every character in A is same as in B then we terms them as equal. eg: If A = 'hello' and B = 'hello' then and only then they both will be termed as equal. 4) Concatenate Concatenation refers to the process of joining strings together. If there are two seperate strings, then after concatenation the two strings will be stiched together and will be termed as one. eg: If A='hello' and B='world' then after concatenation the new string will be 'helloworld'. In this fuction the characters in the array B are copied into array A. A = 'hello'. Therefore A[5] = '\0'. The characters from array B are copied individually to the location staring from A[5]. So now, A[5]=w, A[6]=o and accordingly. So, if the array A is printed it will be printed as 'helloworld'. 5) Reverse In this function we reverse the contents of a string. The string will be reversed and is printed out in reverse order. eg: A = 'hello' then reversed string B will be B = 'olleh'. We start to copy the contents of string A in reverse order i.e. A[ 4 ] = B[ 0 ] , A[ 3 ] = B[ 1 ] , A[ 2 ] = B[ 2 ] and accordingly. The string will then be reversed and is printed. 6) Sub-string Checking for sub-string means to check if the inputed string is the part of the original string. eg: 'llo ' is the sub-string of the string 'hello' 'orl' is the sub-string of the string 'world'. The inputed string will be a sub-string of original string if and only if all the contents of inputed string are present in the original string. i.e. B will be a sub-string of A if and only if all the characters present in B are also present in A. We traverse the entire array A and check if it has the character B[ 0 ]. i.e. the first character of the inputed string. If B[ 0 ] is present in array A then we check for further characters in array B. If all the characters in B are present in A then the search will be successful else B will not be a sub-string of A. The program in menu-driven and will run until the user wishes it to. NOTE - The blog is written by Mr. Mehul Solanki
0 Comments
Leave a Reply. |
AuthorProf. Ketan Sanjay Desale Archives
April 2020
Categories |