Simple String Length Question

Oct 15, 2014 at 2:01am

Why does it only count four characters? How do I get the length of chars in the whole string?

It seems to only stop at the space.
Thanks and greatly appreciated

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 #include <iostream>
#include <string>

using namespace std;



///////////////////// Main Function /////////////////////
typedef char* CharPointer;
int main()
{
	int slength;
	string input;
	CharPointer head,tail;

	cout << "Enter a string : \n";
	cin >> input;
	slength = input.length();
	
	cout << slength;

	return 0;
}
Oct 15, 2014 at 2:43am
cin >> input;

This only reads a word of text. If you want to get a whole line of text, use std::getline().
Oct 15, 2014 at 2:45am
closed account (48T7M4Gy)
Spaces delimit the input. Use getline() if you want to input multiple words or sentences.

http://www.cplusplus.com/doc/tutorial/basic_io/
Oct 15, 2014 at 3:08am
Ok.

Cool thanks guys That works, i forgot about getline() some how.

Isn't there another method that allows you to omit the ' ' spaces?

i thought it was string.substring, but i do not see any methods that takes in a parameter such as

string.substring(string name, regex delim)
Oct 15, 2014 at 3:49am
closed account (48T7M4Gy)
Isn't there another method that allows you to omit the ' ' spaces?


cin.ignore() might be the one you are thinking of but it works in the reverse of that, sort of.

http://www.cplusplus.com/reference/istream/istream/ignore/?kw=cin.ignore
Oct 15, 2014 at 4:33am
yeh,

trying to ignore the spaces for string. basically trying to create a dynamic array with the size of the words itself
Last edited on Oct 15, 2014 at 4:34am
Oct 15, 2014 at 4:44am
closed account (48T7M4Gy)
Still sounds like getline will do the job and all you do is count up the alpha characters in the line to set the size of the array. length() might be the hard way to do the counting.
Oct 15, 2014 at 6:36pm
yeh.

I just wanted to see how you would get rid of the spaces (actually was just curious)
I actually needed the spaces for the program I am trying to implement.

I am actually trying to set a pointer reference the first element in an array, and another pointer to reference the last element of the same array.

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
34
35
36
#include <iostream>
#include <string>

typedef char* CharPointer;
using namespace std;
int main()
{


	CharPointer first,last, diamond;
	string astring;
        int alength;

	cout << "This program reverses a string entered.\n\n"
		 << "Enter a string : \n";

	getline(cin, astring);
	alength = astring.length();
	
	
	diamond = new char[astring.length()]; //creates a dynamic array called diamond
	first = diamond; //references the first element?
	last = diamond + (astring.length()-1); //reference the last element

 //adds a single char to an element of the array	
	for(int i = 0; i < astring.length(); i++)
			diamond[i] = astring[i];   

 //prints the elements in the array(basically reprints //what was inputted)
	for(int i = 0; i < astring.length(); i++)
		cout << diamond[i];
	
		cout <<"\nfirst : " << first;
		cout << "\nlast : " << last;
	return 0;
}

when i cout the pointer 'first' it reprints all of the elements in the array, and the 'last' pointer prints to the last element.

I guess my question is, is this syntax correct the way I have this program. I understand 'first' and 'last' aren't the best names but i chose them for the sake of this example.

I my plan is to actually flip the contents of the array to be printed out reversely using pointers.
I don't want to use any .reverse methods .

Regards
Last edited on Oct 15, 2014 at 6:42pm
Oct 16, 2014 at 12:13am
closed account (48T7M4Gy)
A couple of thoughts:
1. On face value at a quick glance, give it a go.
2. Have you thought of string tokenizers? eg strok();
3. If you are just reversing the line why not leave it as a string and just read the characters in reverse order, character by character, using standard <string> functions? If needs be if a character is a blank then ignore it or whatever. It seems you have overcomplicated the problem but, of course, I might have misunderstood what you are trying to do.
Oct 16, 2014 at 3:07am
1) is my syntax correct for my pointes 'first' and 'last' to point to the first element, and last element in the array called 'diamond'?
2) my plan is to flip the the contents of the array to be reversed using pointers
3)why is my 'first' pointer printing everything in the array? versus the single element diamond[0]?

sorry for any confusion
Oct 16, 2014 at 4:38am
closed account (48T7M4Gy)
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
34
35
36
37
38
#include <iostream>
#include <string>

using namespace std;
int main()
{
	string astring;
	int alength;

	cout << "This program reverses a string entered.\n\n"
		<< "Enter a string : \n";

	getline(cin, astring);
	alength = astring.length();
	cout << "1.  " << astring << '\t' << alength << endl;

	char* diamond = new char[astring.length()];
	cout << "2.  " << diamond << endl;

	char* first = diamond;
	cout << "3.  " << *first << endl;

	char* last = diamond + (alength - 1);
	cout << "4.  " << last << endl;

	for (int i = 0; i < alength; i++)
		diamond[i] = astring[i];
	diamond[alength] = '\0';

	cout << "5.  " << diamond << endl;
	cout << "6.  first: " << first << endl;
	cout << "7.   last: " << last << endl;

	cout << "8.    : " << diamond[5] << endl;
	cout << "9.    : " << diamond[6] << endl;

	return 0;
}


I put in a few diagnostics to make it clearer for me. The reason for the junk is because C-strings require '\0' as the last hidden character to terminate the string. You need to add that last character. Make sure diamond is created to accommodate the extra character.

Also char* diamond (no brackets) is a pointer to the whole array, by definition (new), whereas diamond[x] is the x-th character of that array just as you say.

Hope this helps. :)
Topic archived. No new replies allowed.