Loading...
C++

String Handling Functions

The C++ library has numerous functions for handling C-strings. These functions perform various test and manipulations and required that the cstring header file be included.
The following are the most commonly used string handling functions.,

  1. strcpy() function:
    This function accepts two strings or pointers as arguments and copies the second string to the first string. In simple terms this function is used to copy one string to another.
    Example:
    const int SIZE=13;
    char name[SIZE];
    strcpy(name, “Albert Einstein”);
    The strcpy function’s two arguments are C-string addresses. The contents of the second arguments are copied to the memory location specified by first argument, including the null terminator.
  2. strcat() functions:
    This function accepts two strings or pointers to C-strings as its arguments and concatenates, or appends one string to another.
    Example:
    const int SIZE=13;
    char string1[SIZE] = “HELLO”;
    char string2[] = “WORLD!”;
    cout<<string1<<endl;
    cout<<string2<<endl;
    strcat(string1,string2);
    cout<<string1<<endl;
    OUTPUT:
    HELLO
    WORLD!
    HELLO WORLD!
  3. strlen() function:
    strlen() function counts the number of characters in the given string and returns the integer value. It stops counting the characters when null character is found. Because, null character indicates the end of the string in C++.
    Example:
    #include <cstring>
    char name[] = “Thomas Edison”;
    int length;
    length = strlen(name);
  4. strcmp() function:
    This function takes two C-strings as arguments and returns an integer that indicates how the two strings compare to each other. here is the function’s prototype:
    int strcmp(char *string1, char *string2);
    This function takes two strings as parameters (actually, pointers to C-strings) and returns an integer result. The value of the result is set accordingly:
    • The result is zero if the two strings are equal on a character-by-character basis
    • The result is negative if string1 comes before string2 in alphabetical order
    • the result is positive if string1 comes after string2 in alphabetical order
      Example:
      if(strcmp(string1, string2) ==0)
      cout<<“The strings are equal.\n”;
      else
      cout<<“The strings are not equal.\n”;
  5. strrev() function:
    This function reverses a given string.
    Syntax:
    strrev(string);
    Example:
    strrev(“WebnoidSchools”);
  6. strstr() function:
    This function searches for a string inside a string. For instance, it could be used to search for the string “seven” inside the larger string “Four score and seven years ago”. The function’s first argument is the string to be searched, and the second argument is the string to look for. If the function finds the second string inside the first, it returns the address of the occurrence of the second string within the firts string. Otherwise it returns nullptr (the address 0). Here is an Example:
    char arr[] = “Four score and seven years ago”;
    char *strPtr = nullptr’
    cout<<arr<<endl;
    strPtr = strstr(arr, “seven”); // search for seven”
    cout<<strPtr<<endl;
  7. Strncat() and strncpy() function

Leave a Reply

Your email address will not be published. Required fields are marked *