Wondering How To Make Your String Operations Data Structures In C++ Rock? Read This!

Title: String Operations

Objectives: 

 To implement different operations on strings like Creating, copying, modifying, concatenating, reversing, Finding substrings, etc.  To simulate the inbuilt string operations function.

String: 
A string is a character array terminated by a null character (\0). In C, the null character can be used to mark the end of a string. A string constant is a series of characters enclosed by double-quotes. The C compiler automatically appends a null character to the array that has been initialized by a string constant. char str[7] = "Hello!";

Substring : 
A string is a substring of the main string if it is a part of the main string. 

Palindrome: 
A string is a palindrome if the reverse of the string is equal to the original string. e.g: nitin 

Different string operations are as follows : 

1) int strlen( const char *str) -- Calculates string length excluding last null character. 

2) char * strcpy( char *dest, const char * src) – Copies string from source to destination 

3) char *strcat(char *dest, const char * src ) – Appends source string at the end of destination and returns pointer to the destination.

4) int strcmp(const char *str1, const char * str2) – Does an unsigned comparison of two strings character by character and returns difference as an integer. If diff = 0 strings are equal If diff < 0 string1 is smaller than string2 If diff > 0 string1 is greater than string2

5) char * strrev( char *str) – Reverses the input string and returns a pointer to the reversed string. 

6) char *strstr( char *str1, char *str2) – Checks for string2 in string1 and returns pointer to location of first occurrence. 

7) String palindrome – Checking if reversed string is same as original string. The strlen() Function 

Strlen() Function-

The strlen() function can be used to measure the length of a string. This function does not count the null character in the last element The syntax for the strlen() function is size_t strlen(const char *s); Here s is a char pointer variable. The return value from the function is the number of bytes. size_t is a data type defined in the string.h header file. The size of the data type depends on the particular computer system. 

String Operations Data Structures in C++


Strcpy() Function-

The strcpy() Function If you want to copy a string from one array to another, you can copy each item of the first array to the corresponding element in the second array, or you can simply call the C function strcpy() to do the job for you. The syntax for the strcpy() function is char *strcpy(char *dest, const char *src); Here the content of the string src is copied to the array referenced by dest. The strcpy() function returns the value of src if it is successful. The header file string.h must be included in your program before the strcpy() function is called. 

Strcat() Function-

The strcat() Function strcat appends a copy of src to the end of dest. The length of the resulting string is strlen(dest) + strlen(src). The syntax for the strcat() function is char *strcat(char *dest, const char *src); strcat returns a pointer to the concatenated strings. 

Strrev() Function-

The strrev() Function Reverses all characters in a string (except for the terminating null) The syntax for the strrev() function is char *strrev(char *s); For example, it would change string\0 to gnirts\0 strrev returns a pointer to the reversed string. 

Strcmp() Function-

The strcmp() Function Compares two strings. The string comparison starts with the first character in each string and continues with subsequent characters until the corresponding characters differ or until the end of the strings is reached. The syntax for the strcmp() function is int strcmp(const char *s1, const char*s2); This function returns an int value that is < 0 if s1 < s2 == 0 if s1 == s2 > 0 if s1 > s2 

Strlwr() Function-

The strlwr() Function Converts uppercase letters (A to Z) in string s to lowercase (a to z). The syntax for the strlwr() function is char *strlwr(char *s); No other characters are changed. Return Value is a pointer to the string s. 

Strupr() Function-

The strupr() Function Converts lowercase letters (a to z) in string s to uppercase (A to Z). The syntax for the strupr() function is char *strupr(char *s); No other characters are changed. Return Value is a pointer to the string s. 

Strstr() Function-

The Strstr() Function Finds the first occurrence of a substring in another string The syntax for the strupr() function is char *strstr(const char *s1, const char *s2); strstr scans s1 for the first occurrence of the substring s2. Return Value: On success, strstr returns a pointer to the element in s1 where s2 begins (points to s2 in s1). On error (if s2 does not occur in s1), strstr returns null.

Top 10 Expected Questions Based On String Operations Data Structures Practical Approach: 

Q1. What is a string? How do you know its length? 

Q2. What are the main differences between a string constant and a character constant? 

Q3. Does the gets() function save the newline character from the standard input stream? 

Q4. What types of data can the scanf() function read? 

Q5. What are the left and right values (lvalue & rvalue)? 

Q6. How can you obtain the address of a variable? 

Q7. What is the concept of indirection in terms of using pointers? 

Q8. Can a null pointer point to valid data? 

Q9. What is the main difference between using scanf & gets for accepting strings ? 

Q10. How do you reference an array by using a pointer? 


Design Experiments: 


1. Simulate the following string library functions with pointers to arrays. 

char *strncpy(s,ct,n) Copies at most n characters of string ct to s Returns s. Pads with '\0's if t has fewer than n characters. 

char *strncat(s,ct,n) Concatenates at most n characters of string ct to end of string s; terminates s with '\0'. Returns s. 

int *strncmp(cs,ct,n) Compares at most n characters of string cs to string ct. Returns < 0 if cs < ct 0 if cs= =ct or > 0 if cs > ct 

char *strchr(cs,c) Returns a pointer to the first occurrence of c in cs or NULL if not present. 

char *strrchr(cs,c) Returns a pointer to the last occurrence of c in cs or NULL if not present

2. Simulate the following string library functions with pointers to arrays

size_t strspn(cs,ct) Returns length of prefix of cs consisting of characters in ct.

size_t strcspn(cs,ct) Returns length of prefix of cs consisting of characters not in ct. 

char *strpbrk(cs,ct) Returns pointer to first occurrence in string cs of any character of string ct, or NULL if none present.

char *strstr(cs,ct) Returns pointer to first occurrence of string ct in cs, or NULL if not present. size_t strlen(cs) Returns the length of string cs.  

char *strerror(n) Returns pointer to implementation-defined string corresponding to error n. 

char *strtok(s,ct) strtok searches s for tokens delimited by characters from ct, NULL if none are found. 

Implement above  by dynamically allocating memory for string and perform the various operations on strings with pointers to arrays.

Thus the String Handling Functions (StringCopy, StringLength, StringCompare, StringReverse, StringConcat, SubString) have been Successfully Learned.

Disclaimer: This Site is Protected By Educationworldsystem. Copying of Content Is Strictly Prohibited. 
We Wish to Express Our Profound Thanks to all Those who helped in making this Website a reality

Post a Comment

[blogger]

MKRdezign

Contact Form

Name

Email *

Message *

Powered by Blogger.
Javascript DisablePlease Enable Javascript To See All Widget