Skip to main content

C++ Manipulating Strings

C++ Manipulating Strings
A string is a sequence of character. As you know that C++ do not support built in string type, you have use earlier those null character based terminated array of characters to store and manipulate strings. These strings are termed as C Strings. It often become inefficient performing operations on C strings. Programmers can also define their own string classes with appropriate member functions to manipulate strings. ANSI standard C++ introduces a new class called string which is an improvised version of C strings in several ways. In many cases, the strings object may be treated like any other built in data type. String is treated as another container class for C++.

The C Style String

The C style string belongs to C language and continues to support in C++ also strings in C are one dimensional array of characters which gets terminated by \0 (null character).
This is how the strings in C are declared:
char ch[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
Actually, you do not place the null character at the end of a string constant. The C++ compiler automatically places the \0 at the end of the string when it initializes the array.

String Class in C++

The string class is very huge and includes many constructors, member functions and operators.
Programmers may use the constructors, operators and member functions to achieve the following:
  • Creating string objects
  • Reading string objects from keyboard
  • Displaying string objects to the screen
  • Finding a substring from a string
  • Modifying string
  • Adding objects of string
  • Comparing strings
  • Accessing characters of an string
  • Obtaining the size or length of string, etc…

Manipulate Null-terminated strings

C++ supports a wide range of functions that manipulate null-terminated strings. These are:
  • strcpy(str1, str2): Copies string str2 into string str1.
  • strcat(str1, str2): Concatenates string str2 onto the end of string str1.
  • strlen(str1): Returns the length of string str1.
  • strcmp(str1, str2): Returns 0 if str1 and str2 are the same; less than 0 if str1<str2; greater than 0 if str1>str2.
  • strchr(str1, ch): Returns a pointer to the first occurrence of character ch in string str1.
  • strstr(str1, str2): Returns a pointer to the first occurrence of string str2 in string str1.

Important functions supported by String Class

  • append(): This function append a part of string to another string
  • assign():This function assigns a partial string
  • at():This function obtains the character stored at a specified location
  • begin():This function returns a reference to the start of the string
  • capacity():This function gives the total element that can be stored
  • compare():This function compares string against the invoking string
  • empty(): This function returns true if the string is empty
  • end():This function return a reference to the end of the string
  • erase():This function remove character as specified
  • find():This function searches for the occurrence of a specified sub string
  • length(): It gives the size of string or the number of elements of a string
  • swap():This function swaps the given string with the invoking one

Important Constructors obtained by String Class

  • String(): This constructor is used for creating an empty string
  • String(const char *str): This constructor is used for creating string objects from a null terminated string
  • String(const string *str): This constructor is used for creating a string object from another string object

Operators used for String Objects

  1. =: assignment
  2. +: concatenation
  3. ==: Equality
  4. !=: Inequality
  5. <: Less than
  6. <=: Less than or equal
  7. >: Greater than
  8. >=: Greater than or equal
  9. []: Subscription
  10. <<: Output
  11. >>: Input

Comments

For Programs Click Here

Popular posts from this blog

Syllabus

Syllabus  C Programming Tutorials C Tutorial C Introduction History of C Programming Language C Installation C Program Structure C Input and Output (I/O) C Format Specifiers Declaration & Assignments C Tokens C Identifiers C Keywords C Constants C Operators C Data Types C Variables C Preprocessors C Type Casting C Custom Header File Flow Control C Decision Making C if Statements C if-else Statements C Nested if-else Statements C else-if Statements C goto Statement C switch Statements C Loops C while loops C do while loops C for loops Functions C Functions C Function Arguments C Library Functions C Variable Scope Arrays & Strings C Arrays C Strings Pointers C Pointers C Dynamic Memory Allocation Structure & Union C Structures C Unions File I/O C File Handling C fopen C fclose C getc C putc C getw C putw C fprintf C fscanf C fgets C fputs C feof                                     ...

Syllabus

Python Tutorials Python Tutorial Python Overview Python Installation Basics of Python Programming Python Operators Python Keywords Python Numbers Python Strings Python Data Types Python Variables Python Lists Python Tuples Python Date and Time Python Decision Making Python Loops Python File Handling Python Dictionaries Python Functions Python Modules Python Exceptions Handling Python Object Oriented Inheritance in Python Python Regular Expressions Python Networking Programming Python Multithreaded Programming Python CGI Programming Python Database Connection Python Metaprogramming Python Data Processing And Encoding Python GUI Programming

C++ Data Abstraction

C++ Data Abstraction Object oriented programming offers various features to write programs with various concepts that help to minimize problems and increase flexibility in program. One of the features of object oriented programming is Data abstraction. In this chapter you will learn about how the concept data abstraction is carried out within the C++ program. What is Data abstraction? Data abstraction allows a program to ignore the details of how a data type is represented. Abstraction (derived from a Latin word  abs , meaning away from and  trahere , meaning to draw) refers to the act of representing essential features without including the background details or explanations. C++ classes use the technique of abstraction and are defined as a list of abstract attributes such as width, cost, size etc and functions to operate on these attributes. They put in a nutshell all the essential properties of an object that are required to be created. The attributes are therefore called...