How can i find a char value's length?

I tried this.This is run.But it is taking a system or another error:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;
char *a;
int main()
{cout<<"Write ur text: ";
cin>>a;
int b;
char c[b];
a=new char[b];
cout<<"Ur text length: "<<sizeof(c)<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

I want to write b value or a length with this method or another method in console.But i don't want like this method:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

using namespace std;
char *a;
int main()
{string b;
    cout<<"Write ur text: ";
cin>>a;
b=string(a);
cout<<"Ur text length: "<<b.length()<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

Can i use without string method?
Last edited on
I think you're looking for strlen().
Your programs are seriously broken. The main issue is that you are trying to input into non-allocated space, or are trying to do non-standard space allocations. Also, avoid using system() for stuff like that.

Here's a little help.

C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
  char s[ 1000 ];
  fgets( s, 1000, stdin );

  size_t n = strlen( s );
  if (s[ n - 1 ] == '\n') s[ --n ] = '\0';
  printf( "%s%d%s\n", "You entered ", n, " characters." );

  printf( "Press ENTER to quit." );
  fflush( stdout );
  while (getchar() != '\n');
  return EXIT_SUCCESS;
}


C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <limits>
#include <string>
using namespace std;

int main()
{
  string s;
  cout << "Type something and press ENTER: ";
  getline( cin, s );

  cout << "You entered " << s.length() << " characters.\n";

  cout << "Press ENTER to quit." << flush;
  cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
  return 0;
}

Hope this helps.
Thank u for ur little help Duoas.But this is not my question's answer.If my char value bigger than 1000 this method doesn't work.But if i write Max char length instead 1000 this is work.Is this work?
How is max char length?
Last edited on
It's impossible for a char to have a value of over 1000, or 255 for that matter.
I find this:65792.Ok.Thank u.
I sweat anything.Can i find this without strlen or another func?Like this my false method:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main()
{int n=65792;
char *b;
cout<<"Write ur text: ";
cin>>b;
b=new char[n];
if(b[ n - 1 ] == '\n') b[ --n ] = '\0';
cout<<"Ur text length: "<<n<<endl;
    return 0;
}


Last edited on
If my char value bigger than 1000 this method doesn't work.

That's right, the fgets() function only accepts n-1 characters as input. If you want something fancy, capable of inputting strings of any length in C, then you need a fancier function.

The GNU readline() function is a very good one, but you can also create your own, as I did in this thread:
http://www.cplusplus.com/forum/beginner/4174/#msg18271

BTW, as ascii indicated, a char is always one character long (as it is a single character), but an array of characters is a "string".

1
2
3
char  c;         /* This is a single character */
char  s[ 100 ];  /* This is a "string" of characters. */
char* p;         /* This can point to either a single character or to a string of characters. */

Hope this helps.
How fgets() func do control this?I sweat this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main()
{int n=65792,i,z;
char c[n];
char *d="";
cout<<"Write ur text: ";
cin>>c;
for(i=0;i<n;i++)
{
if((int)d==(int)c[i])
        z=n-i;
        }
cout<<"Ur text length:"<<n-i<<endl;      
system("pause");
  return 0;
}


This method is not working too.
Last edited on
Topic archived. No new replies allowed.