Hey, I'm not sure how to do this. I know how to convert a char into an int, getting the ascii value, or the numeric value, but that's not what I want to do.

Basically I have a CHAR, I give it a value, usually a short sentence, then I display it on screen, but I have a boarder thats dependent on the length of that sentence, so if I could get an interger representing the length (how many characters), that would help me A LOT.

How can I get it to convert for example like, the CHAR is "Hello There" after converting to an INT it should output 11, as there are 11 characters including the space...

Hopefully you guys understand, anyone able to help me out?

Thanks so much!

Dani AI

Generated

Short answer: use the library length functions rather than trying to "convert" characters to an int. As noted, C has strlen and C++ has std::string::size() / std::string::length(). The manual loop posted by (and the single-line variant from ) demonstrates the idea, but the standard functions are simpler and safer. These functions return size_t; if you really need an int, cast carefully and check for overflow.

A minimal modern C++ workflow (reads a whole line and gets its length):

#include <iostream>
#include <string>

std::string s;
std::getline(std::cin, s);
std::size_t n = s.size();        // number of bytes/characters for simple ASCII
std::cout << n << '\n';

Use std::getline instead of the deprecated gets (the latter is unsafe and removed from modern C). See std::string::size/length and strlen for details, and avoid gets as explained here: gets (removed/unsafe).

Notes and cautions:

  • If your buffer is a fixed char arr[50], sizeof(arr) gives buffer size (50), not the string length; use strlen or std::string::size() instead.
  • size_t is the correct type for lengths; only convert to int when necessary and after checking bounds.
  • If input may be UTF-8, .size() or strlen reports bytes, not user-perceived characters (grapheme clusters). For correct Unicode character counting, use a Unicode-aware library (for example, ICU).

Recommended Answers

All 6 Replies

It almost sounds like you are trying to calculate the length of a string like strlen does.

Ugh, one of my half retarded days. lol

Thanks.

Ugh, one of my half retarded days. lol

Thanks.

We all have those kind of days on occasion. :)

/* Wap to input a string and calculate its length  */
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
clrscr();
int len=0,i;
char str[50];
cout<<"**** Atishay c++ programming LTD. ****" <<endl<<endl;
cout<<"Input a string (word)"<<endl;
gets(str);
for(i=0;str[i]!='\0';i++)
{
len++;
}
cout<<"Your entered string has length of  "<<len<<endl;
getch();
}
for(i=0;str[i]!='\0';i++)
{
len++;
}

you could optimize that loop like this:

for(len = 0; str[len] != 0; len++)
   ;

ooooooh gud idea....thax.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.