Logo

Navigation
  • Home
  • Services
    • ERP Solutions
    • Implementation Solutions
    • Support and Maintenance Solutions
    • Custom Solutions
    • Upgrade Solutions
    • Training and Mentoring
    • Web Solutions
    • Production Support
    • Architecture Designing
    • Independent Validation and Testing Services
    • Infrastructure Management
  • Expertise
    • Microsoft Development Expertise
    • Mobile Development
    • SQL Server Database and BI
    • SAP BI, SAP Hana, SAP BO
    • Oracle and BI
    • Oracle RAC
  • Technical Training
    • Learn Data Management
      • Business Intelligence
      • Data Mining
      • Data Modeling
      • Data Warehousing
      • Disaster Recovery
    • Learn Concepts
      • Application Development
      • Client Server
      • Cloud Computing Tutorials
      • Cluster Computing
      • CRM Tutorial
      • EDI Tutorials
      • ERP Tutorials
      • NLP
      • OOPS
      • Concepts
      • SOA Tutorial
      • Supply Chain
      • Technology Trends
      • UML
      • Virtualization
      • Web 2.0
    • Learn Java
      • JavaScript Tutorial
      • JSP Tutorials
      • J2EE
    • Learn Microsoft
      • MSAS
      • ASP.NET
      • ASP.NET 2.0
      • C Sharp
      • MS Project Training
      • Silverlight
      • SQL Server 2005
      • VB.NET 2005
    • Learn Networking
      • Networking
      • Wireless
    • Learn Oracle
      • Oracle 10g
      • PL/SQL
      • Oracle 11g Tutorials
      • Oracle 9i
      • Oracle Apps
    • Learn Programming
      • Ajax Tutorial
      • C Language
      • C++ Tutorials
      • CSS Tutorial
      • CSS3 Tutorial
      • JavaScript Tutorial
      • jQuery Tutorial
      • MainFrame
      • PHP Tutorial
      • VBScript Tutorial
      • XML Tutorial
    • Learn Software Testing
      • Software Testing Types
      • SQA
      • Testing
  • Career Training
    • Career Improvement
      • Career Articles
      • Certification Articles
      • Conflict Management
      • Core Skills
      • Decision Making
      • Entrepreneurship
      • Goal Setting
      • Life Skills
      • Performance Development
      • Personal Excellence
      • Personality Development
      • Problem Solving
      • Relationship Management
      • Self Confidence
      • Self Supervision
      • Social Networking
      • Strategic Planning
      • Time Management
    • Education Help
      • Career Tracks
      • Essay Writing
      • Internship Tips
      • Online Education
      • Scholarships
      • Student Loans
    • Managerial Skills
      • Business Communication
      • Business Networking
      • Facilitator Skills
      • Managing Change
      • Marketing Management
      • Meeting Management
      • Process Management
      • Project Management
      • Project Management Life Cycle
      • Project Management Process
      • Project Risk Management
      • Relationship Management
      • Task Management
      • Team Building
      • Virtual Team Management
    • Essential Life Skills
      • Anger Management
      • Anxiety Management
      • Attitude Development
      • Coaching and Mentoring
      • Emotional Intelligence
      • Stress Management
      • Positive Thinking
    • Communication Skills
      • Conversation Skills
      • Cross Culture Competence
      • English Vocabulary
      • Listening Skills
      • Public Speaking Skills
      • Questioning Skills
    • Soft Skills
      • Assertive Skills
      • Influence Skills
      • Leadership Skills
      • Memory Skills
      • People Skills
      • Presentation Skills
    • Finding a Job
      • Etiquette Tips
      • Group Discussions
      • HR Interviews
      • Interview Notes
      • Job Search Tips
      • Resume Tips
      • Sample Resumes
 

C++ String Representation and Handling

By Sripriya R | on September 8, 2007 |
C++ Tutorials

In this C++ tutorial, you will learn about string representation and handling, how a string is represented, end string notation, initializing char array, character representation, string literals, example program to understand the character array concept and the accessing of character array in detail and how to represent an array of Strings.

How is a string represented?

Strings, or sequences of characters, are represented as arrays of char elements. For example, when a programmer wants to represent a char array Exforsys having 30 elements it is declared:

char Exforsys[30];

This means the character array has 30 storage location allocated where it can store up to a maximum of 30 characters.

End String Notation

The character array can store the specified maximum of array limit. This means it can store less than the allocated array limit up to the maximum. It is essential that a notation exist to mark the end of character array or string representation. This end of character array is denoted using backslash zero ‘’.

The Differences between Character Constants and String Literal:

Initializing Char array

Suppose the programmer wants to initialize the array char Exforsys[30] with value Training. This initialization of char array can be performed using two methods:

  • Character representation
  • String Literals

The character representation is notated as follows:

char Exforsys[30]= {‘T’,’r’,’a’,’i’,’n’,’i’,’n’,’g’,’’};

The string literal representation is notated as follows:

char Exforsys[30]= “Training”;

Character representation uses single quotes to represent each character while string literal representation uses double quotes for the entire string literal.

Another main difference with character representation is the end of character denoted by ‘’. String literal representation uses double quotes; there is no need for representing the end of character. The end of character task is automatically performed by C++ programming language.

It is also possible to initialize array as follows:

char Exforsys[ ]=”Company”;

In the above case, the char array Exforsys is not mentioned with the size and is initialized with the string literal Company. The character array Exforsys takes the size of the initialized string literal.

An Example:

Input string defined as character array with the same output on the display screen:

  1.  #include <iostream> 
  2. using namespace std;
  3. void main( )
  4. {
  5. char Exforsys[80];
  6. cout << "Input the String: ";
  7. cin >> Exforsys;
  8. cout << "The string entered is: " << Exforsys;
  9. }

The output of the above program is

Detailed example to understand the concept of character array and accessing of character array:

The below example uses two input strings as two character arrays, accessing one character array character by character, then copies it into the other character array.

  1.  #include <iostream> 
  2. using namespace std;
  3.  #include <string.h>
  4. void main( )
  5. {
  6. 	char examp1[] = "Exforsys";
  7. 	char examp2[80];
  8. 	for(int i=0; i < strlen(examp1);i++)
  9. 	{
  10. 		examp2[i]=examp1[i];
  11. 	}
  12. 	int z = strlen(examp1);
  13. 	examp2[z]='';
  14. 	cout<< "The copied string is: " << examp2;
  15. }

The output of the above program is

In the above program, the string function strlen is used. This string function is used to find the length of the string given in the argument of the function. In this case, it finds the length of the string Exforsys. This predefined special string function strlen is present in the header file string.h. Therefore, this is also included in the program.

In this example, the first string initializes with string literal Exforsys and the second literal declares as an empty string. The for loop uses the first character from the first character array examp1[] and copies it into the first position in the second character array examp2[]. The loop continues until the end of string value is reached. The second character array is terminated by end of character and then printed.

The above example provides the programmer with insight on how to declare character arrays, how to initialize character arrays and how to access character arrays.

The above example can also be performed using a predefined string function called strcpy.

The string function strcpy takes two strings as argument.

The general syntax is as follows:

strcpy(string1,string2);

The string function strcpy copies the string2 (the second argument) into string1 (the first argument).

How to represent array of Strings:

It is possible to declare two-dimensional arrays in character arrays or array of strings.

The general syntax is as follows:

char arrayname[numb1][numb2];

In this numb1 denotes the number of string literals defined in the array and numb2 defines the maximum length of characters of the strings.

For example, if the programmer wants to represent a char array Exforsys with string literals as:

Training

Forums

Program

Then numb1 takes the value 3 and numb2 takes the value 8 because Training has the maximum character 8. The representation is as follows:

char Exforsys[3][8];

The access of the character array can be performed by using for loop.

« « Working with XML in C
C++ Operator Overloading Part II » »

Author Description

Avatar

Free Training

RSSSubscribe 394 Followers
  • Popular
  • Recent
  • C++ Manipulators

    August 27, 2007 - 0 Comment
  • C++ Functions

    September 21, 2006 - 0 Comment
  • C++ Virtual Functions

    September 25, 2007 - 0 Comment
  • C++ Decision Making Statements

    September 1, 2007 - 0 Comment
  • C++ Functions with Arguments

    September 21, 2006 - 0 Comment
  • Object Oriented Programming Paradigm

    June 10, 2006 - 0 Comment
  • C++ Pure Virtual Function and Base Class

    September 28, 2007 - 0 Comment
  • C++ Looping

    September 4, 2007 - 0 Comment
  • C++ Storage Classes

    September 10, 2007 - 0 Comment
  • Elements of Object Oriented Programming

    June 10, 2006 - 0 Comment
  • C++ Dereference Operator

    October 14, 2007 - 0 Comment
  • C++ Memory Management Operators

    October 12, 2007 - 0 Comment
  • C++ Void Pointer and Null Pointer

    October 12, 2007 - 0 Comment
  • C++ Pointers

    October 1, 2007 - 0 Comment
  • C++ Static Functions

    October 1, 2007 - 0 Comment
  • C++ Friend Functions

    September 28, 2007 - 0 Comment
  • C++ Pure Virtual Function and Base Class

    September 28, 2007 - 0 Comment
  • C++ Virtual Functions

    September 25, 2007 - 0 Comment
  • C++ Inline Functions

    September 25, 2007 - 0 Comment
  • C++ Function Passing Types

    September 12, 2007 - 0 Comment

Exforsys e-Newsletter

ebook
 

Related Articles

  • C++ Dereference Operator
  • C++ Memory Management Operators
  • C++ Void Pointer and Null Pointer
  • C++ Pointers
  • C++ Static Functions

Latest Articles

  • Project Management Techniques
  • Product Development Best Practices
  • Importance of Quality Data Management
  • How to Maximize Quality Assurance
  • Utilizing Effective Quality Assurance Strategies
  • Sitemap
  • Privacy Policy
  • DMCA
  • Trademark Information
  • Contact Us
© 2023. All Rights Reserved.IT Training and Consulting
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.AcceptReject Read More
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT