strlen and sizeof

Pages: 12
Sorry, its 5 in the morning here, so I am really tyred.
Last edited on
No, atoi didnĀ“t work.
This is getting old. What are you trying to do? What is your objective?
I sad that before. I am trying to put 0 into the string. Which when I return , I wont get data only until the 0 is found, but the whole string: padding + 0 + message.
Last edited on
normal c string functions like strcat/strcpy/strcmp/etc will all fail with these strings because they look for the first 0 and treat that as the end of the string. If you want to represent strings this way you'll probably need to write your own series of functions.

Or you might be able to use std::string. But even then, when you use c_str() to get the c string, that will have the same problem.

A more intelligent approach would be to not store the string this way, and instead have multiple strings in a data structure or class of some kind.

I'm assuming the data needs to be output to a file/user at some point... when that needs to be done, you can "convert" your series of strings in to the necessary format.
its probably becasue the char of number 2 is 2


No. The char of 'number 2' is not 2. It is a non-printable character STX (start of text, a control character).

Look up an ASCII chart, and it will show you the corresponding decimal (integer) values for all the ASCII characters. The ASCII character 0 is decimal value 48.

The following code gets the following output:
1
2
3
4
5
6
7
8
char x = 48;
if(x == '0')
  cout << "X is zero" << endl;

x = '0';
if(x == 48)
  cout << "X is still zero" << endl;
X is zero
X is still zero


Or to use your own example:
1
2
char x = '2';
x == 50; //TRUE 



Id go this way about it. I believe the OP wants to read a string, and break it into 2.
padding + 0 + message.


ABC0XYZ <------= "ABC", "0", "XYZ" using c_str I'd read a for each and when I get a comparison equal to 0, i'd store the previos elements 0-i in my pre variable and then i-end in my post variable.

length is easy to get using string or whatever...
Last edited on
From my understanding, he wants to put a number character in a string given that number as an integer.

Hell, just go
1
2
3
int x;
cin >> x; //he types a digit
str[MID_STRING]=(char)(x+48);


This is the most heavy-duty and cheap method, but it'll work. For single digits! Two digits and peoples go crazy!

Well, actually, it'd substitute a pre-existing character of the string, so you'd have to shift everything as of MID_STRING one space to the right (making sure your array/vector has the space for it) first, but that's a subject for another day.

Or, you know, you could be reasonable about it.
1
2
3
char x;
cin >> x;
str[MID_STRING]=x;


Sure, you'll write 2, but since x is a char, it'll think you mean '2'.

Or, if you absolutely must take it in as an integer... wait, no, it's always easier to take it as a string.

atoi() will accept a string and spit it out as an integer value (also works for >1 digit!), so if you need to do math with the value, you can just go
1
2
int y = atoi(x)/2;
strcpy_s(&str[MID_STRING],REMAINING_SPACE,x);

To go the other way around, it's _itoa_s(), but it's far less usable, since it doesn't return the string, but injects it into a pointer given as an argument, meaning there's a lot more work involved.
1
2
3
4
5
y=x/2;
char* str2 = new char[20];
_itoa_s(x,str2,20,10);
strcpy(&str[MID_STRING],REMAINING_SPACE,str2);
delete[] str2;


So yes, should you ever need to work with a value both as an integer (for SCIENCE!) and as a character/string (for... a liberal arts degree?), always keep it as a character/string. If for no other reason than because atoi() > _itoa_s().
This was kind of fun to work on, even if it might be way off base as to what he wants :),
the problem with my code is that you can't enter'0' into the body of the text you want to
keep or else that will signal the program to disregard what is before it,
which was the original intention of the previous '0':anyway~~ here is what I came up with.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

#include <iostream>
using namespace std;

int main()

{
	char a[200]; int h[1200]; int f,i,j;f=0;
	cout <<"enter your string to be converted to numerical value.\n\n";
	gets(a); 
	
	cout << "\n";
	
	cout << a << "\n\n"; //display string with 'padding'
	
	for (j=0;j<=strlen(a)-1;j++) {h[j] = a[j]; //convert from string to numerical data.
		cout <<h[j]; }// display numerical data with 'padding'
	
	cout << "\n";

	//reconvert numerical data to characters, removing 'padding'
	for (j=0;j<=strlen(a)-1;j++) {if (h[j] == 48){i = 0;f=1;} a[i] = h[j]; i=i+1;}	

																					cout << "\n";
	
	for (j=f;j<=i-1;j++){ //remove '0' from beginning of string and ending at end of input
		cout <<a[j];}//display final output

	cout << "\n";

	return 0;
}






qwerty0hello world.

113119101114116121481041011081081113211911111410810046

hello world.




Last edited on
If you want to represent strings this way you'll probably need to write your own series of functions.


And how is this posible? I cant imagine, what functions like that should do.

A more intelligent approach would be to not store the string this way, and instead have multiple strings in a data structure or class of some kind.


Yes, that is the way as well, but the point is, data must be together in one string.

BettyBoopTS, your program wasnt working. The line 14 doesnt show the data with padding. I cant convert it to int. It must be char.

I know, it maybe doesnt make sence, but that is the way like that padding works in reall.





Last edited on
Topic archived. No new replies allowed.
Pages: 12