Close Menu

    Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    What's Hot

    How to use hooks inside React Class Components

    November 29, 2023

    Experience at CNCF – Jaeger under the LFX Mentorship Program

    November 29, 2023

    Wrapping up the first month at CNCF – Jaeger under LFX Mentorship

    November 29, 2023
    Facebook X (Twitter) Instagram
    • Our Authors
    Facebook X (Twitter) Instagram Pinterest Vimeo
    HackTechHub
    • Homepage
      • Advisory Team
      • Charter for Advisory Team
    • Events
    • Coding
      • C++
      • Competitive Programming
        • Mathematics
      • Algorithms
        • Arrays
        • Linked List
        • Mathematical Algorithms
      • DSA
        • Array
        • Graphs
        • Hashing
        • Linked List
        • Recursion
        • Sorting Algorithms
        • String
        • Tree
    • Development
      • Web Development
        • Backend Development
        • Frontend Development
    • DevOps
      • Docker
      • Kubernetes
    • Security
      • Ethical Hacking
      • Authentication & Security
      • Networking
      • Linux
    • Finance
      • Cryptocurrency
      • Fiat
    Subscribe
    HackTechHub
    Home » Introduction to String Data Type
    Coding

    Introduction to String Data Type

    HackTechHubBy HackTechHubApril 30, 2022Updated:June 14, 2022No Comments2 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Introduction to String
    Share
    Facebook Twitter LinkedIn Pinterest Email

    String is basically a sequence of characters. As we know, we already have a character array so why do we need a separate data type for string . The reason that makes string special is the small set of characters. We typically encounter small set of characters in string. There are 128 standard ASCII values that cover most of the character that we generally see. For example English alphabets ‘a’ to ‘z’ and ‘A’ to ‘Z’.

    Multiple languages encode the char data type in different ways. In c and c++, the char data type uses ASCII and stores character using 8 bits while in Java, char data type uses UTF 16 and is stored using 16 bits.

    ASCII stands for American Standard Code for Information Interchange. ASCII value of some characters are shown below.

    UTF-16 is a character encoding capable of encoding all 1,112,064 valid character code points of Unicode.

    The first 128 characters and their encoding have same ASCII value and UTF-16 value.

    In C++, if we assign a character to an integer and print it’s value, we will get the corresponding ASCII value of character as shown below.

    int main()
    {
        char x = 'a';
        cout<<(int)x;
        return 0;
    }

    The output for the above code would be 97, as the ASCII value for character ‘a’ is 97, as shown in the table above.

    Let us see an example to understand this concept.

    Write a program to print frequencies of character in a string of lowercase alphabets in sorted order.

    In C++:

    In Java:

    In the above code, we create an array of size 26 (i.e for each lowercase letter) and initialise this array as 0. Thereafter, we traverse through the string and use individual character of string to locate its index and then increment the corresponding count. Index 0 corresponds to a, index 1 corresponds to b and so on.

    When you use character in a context where we should be using integer, they are automatically converted into integer corresponding to ASCII value in c++ and UTF-16 in java.

    In order to obtain the frequency of the characters, we then traverse the count array and print characters having count greater than 0.

    This Article Was Contributed By Ritika Semwal

    DSA String
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleIntroduction to Simple Linked List in C++
    Next Article Rabin-Karp Algorithm for Pattern Searching
    HackTechHub
    • Website

    HackTechHub Official Editorial Staff.

    Related Posts

    C++

    Linked List Data Structure

    October 6, 2022
    C++

    Tower of Hanoi – Recursion Based Problem

    August 23, 2022
    Algorithms

    Combinatorial Game Theory | Game of Nim

    August 11, 2022
    Add A Comment

    Leave A Reply Cancel Reply

    Demo
    Top Posts

    Experience at CNCF – Jaeger under the LFX Mentorship Program

    November 29, 202312 Views

    Wrapping up the first month at CNCF – Jaeger under LFX Mentorship

    November 29, 202312 Views

    How to Implement SSO in a NodeJS App

    April 24, 20237 Views
    Stay In Touch
    • Facebook
    • YouTube
    • TikTok
    • WhatsApp
    • Twitter
    • Instagram
    Latest Reviews

    Subscribe to Updates

    Get the latest tech news from FooBar about tech, design and biz.

    Demo
    Most Popular

    Experience at CNCF – Jaeger under the LFX Mentorship Program

    November 29, 202312 Views

    Wrapping up the first month at CNCF – Jaeger under LFX Mentorship

    November 29, 202312 Views

    How to Implement SSO in a NodeJS App

    April 24, 20237 Views
    Our Picks

    How to use hooks inside React Class Components

    November 29, 2023

    Experience at CNCF – Jaeger under the LFX Mentorship Program

    November 29, 2023

    Wrapping up the first month at CNCF – Jaeger under LFX Mentorship

    November 29, 2023

    Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    Facebook X (Twitter) Instagram Pinterest
    • Our Authors
    © 2023 CodifyPlus. Designed by CodifyPlus | Ansh.

    Type above and press Enter to search. Press Esc to cancel.