Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Character?
3.
What is String?
4.
Difference Between String and Character
5.
Frequently Asked Questions
5.1.
How many letters can be enclosed in a character?
5.2.
How many possible ASCII values can be stored in a character?
5.3.
What is the difference between an array and a string?
6.
Conclusion
Last Updated: Oct 7, 2024
Easy

Difference Between String and Character

Author Rajat Agrawal
0 upvote

Introduction

In programming languages, we very often hear about the terms Strings and Characters. In programming, knowing the difference between strings and characters is crucial. Characters represent individual symbols like letters or digits, while strings are sequences of characters. Understanding these distinctions helps in effectively handling and manipulating text data in various applications.

Also see, Longest Common Substring and Rabin Karp Algorithm

What is Character?

Characters are simple alphabets such as a, b, c, d...., but with one exception. Any single-digit integer, such as 0, 1, 2, 3, 4,..., and special characters, such as $,%, +, -,*,..., etc., are similarly treated as characters in computer programming, and you can assign them to a character type variable by simply enclosing them in single quotes.

Character

For example, we declare a character type variable ch and assign it the value 'a'.

char ch = 'a';


Here, 'a' is referred to as a character literal or a character constant, and 'ch' is a character type variable that can store a character from the implementation's character set. Not only  a, b, c, d...., etc., but also numbers like 0, 1, 2, 3, 4..., etc., or special characters like $,%, +, -,*..., etc. will be considered as character literals and can be assigned to a variable of character type, making the following point valid.

char ch = '$';


A character data type takes up 8 bits of memory; therefore, you can store any 256 possible values in a character whose ASCII value is between -127 and 127.

Any character, including special characters like !, @, #, $,%, &, *,_, +..., etc., can be stored in a character data type.

Note that only one letter or one digit can be enclosed in single quotations; multiple letters or numbers cannot be enclosed in single quotes. As a result, the following statements are incorrect in C/C++ programming:-

char ch = 'abc';

char ch = '123';


Let’s learn about Strings. 

Read More About, Data Structures and Algorithms and Data Structure.

What is String?

Strings are similar to sentences. They refer to a sequence of characters represented as a single data type. They're made up of a list of characters, which is actually an "array of characters." When passing information from the program to the user, strings come in quite handy. When storing data for the computer to use, they are less useful.

String

In C++, we can represent a string by declaring it using a string keyword or by making a character array.

Let’s see the following example to understand how to declare a string in C++:

/* String */
string str = "Ninjas";

/* Character Array */
char str[7] = ['N', 'i' ,'n' ,'j', 'a', 's', '\0']; 


Let’s learn the difference between string and character.

Also check out - Substr C++

Also see, Morris Traversal for Inorder.

Difference Between String and Character

The main difference between string and character is described in the following table.

Parameter String Character
Definition Sequence of characters Single character
Data Type Typically a class or data structure Primitive data type
Representation Enclosed in double quotes Enclosed in single quotes
Usage Handling text and multiple characters Storing and manipulating single symbols
Length Variable (depends on content) Always 1
Example "Hello" 'A'

Check out this article - C++ String Concatenation and Application of Graph in Data Structure

Frequently Asked Questions

How many letters can be enclosed in a character?

Only one letter or one digit can be enclosed in single quotations; multiple letters or numbers cannot be enclosed in single quotes.

How many possible ASCII values can be stored in a character?

A character data type takes up 8 bits of memory; therefore, you can store any 256 possible values in a character whose ASCII value is between -127 and 127.

What is the difference between an array and a string?

An array is a linear data structure that holds a collection of elements with the same data type. A string is a collection of characters defined as an object.

Conclusion

Comprehending the differences between strings and characters is crucial for effective programming. Strings are sequences of characters enclosed in double quotes, used for handling text and varying in length. Characters, represented by single symbols in single quotes, are primitive data types primarily used for storing and manipulating individual symbols. 

In this article, we have extensively discussed Strings, characters, and the difference between string and character. I hope you enjoyed reading this article.

If you want to learn more, check out our articles:

Recommended problems -

Live masterclass