Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8367437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:09:30+00:00 2026-06-09T13:09:30+00:00

Possible Duplicate: C++ String Length? I really need a help now. How to accept

  • 0

Possible Duplicate:
C++ String Length?

I really need a help now. How to accept string as input and find the length of the string? I just want a simple code just to know how it works. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T13:09:31+00:00Added an answer on June 9, 2026 at 1:09 pm

    You can use strlen(mystring) from <string.h>. It returns the length of a string.

    Remember: A string in C is an array of chars which ends in character ‘\0’. Providing enough memory is reserved (the whole string + 1 byte fits on the array), the length of the string will be the number of bytes from the pointer (mystring[0]) to the character before ‘\0’

    #include <string.h> //for strlen(mystring)
    #include <stdio.h> //for gets(mystring)
    
    char mystring[6];
    
    mystring[0] = 'h';
    mystring[1] = 'e';
    mystring[2] = 'l';
    mystring[3] = 'l';
    mystring[4] = 'o';
    mystring[5] = '\0';
    
    strlen(mystring); //returns 5, current string pointed by mystring: "hello"
    
    mystring[2] = '\0';
    
    strlen(mystring); //returns 2, current string pointed by mystring: "he"
    
    gets(mystring); //gets string from stdin: http://www.cplusplus.com/reference/clibrary/cstdio/gets/
    

    http://www.cplusplus.com/reference/clibrary/cstring/strlen/

    EDIT: As noted in the comments, in C++ it’s preferable to refer to string.h as cstring, therefore coding #include <cstring> instead of #include <string.h>.

    On the other hand, in C++ you can also use C++ specific string library which provides a string class which allows you to work with strings as objects:

    http://www.cplusplus.com/reference/string/string/

    You have a pretty good example of string input here: http://www.cplusplus.com/reference/string/operator%3E%3E/

    In this case you can declare a string and get its length the following way:

    #include <iostream>
    #include <string>
    
    string mystring ("hello"); //declares a string object, passing its initial value "hello" to its constructor
    cout << mystring.length(); //outputs 5, the length of the string mystring
    cin >> mystring; //reads a string from standard input. See http://www.cplusplus.com/reference/string/operator%3E%3E/
    cout << mystring.length(); //outputs the new length of the string
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: how to sort by length of string followed by alphabetical order? I
Possible Duplicate: Find text string using jQuery? How do you find a text string
Possible Duplicate: Converting string in C# I want to camelize a string, for example:
Possible Duplicate: Maximum length of a std::basic_string<_CharT> string I would like to know how
Possible Duplicate: Converting a four character string to a long I want to convert
Possible Duplicate: PHP: How to generate a random, unique, alphanumeric string? i want to
Possible Duplicate: String vs StringBuilder I just revisited some of the books that I
Possible Duplicate: String equality vs equality of location I just got my first assessed
Possible Duplicate: Split String into smaller Strings by length variable I have seen solutions
Possible Duplicate: Difference between WIN32 and other c string I got this code inside

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.