array of chars with a space

if i am looking to increment through an array of chars but stop at a space, how do i do this?
here is what i have, but it's not working
my end objective is to print the first name then last name, then last name then first name.
so, i want to print
"tom simms"
and then
"simms tom"


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void printName(char fullName[], int);

int main()
{
	char fullName[] = {'t', 'o', 'm', ' ', 's', 'i', 'm', 'm', 's', '\0'};
		
	printName(fullName, 10);

	system("pause");
	return 0;
}
/****
printName
****/
void printName(char name[], int)

{
	for (int count = 0; count < ' '; count ++)
		cout << name[count];
}

What do you mean by "stop at a space?" If you want it to print "tom simms," would you not need to print the space still?

As per your end objective to print out the name forward and reversed, perhaps you could use a for loop to check if each char is a space, and once one is found, make a boolean true, which switches the chars being added to two separate strings to hold the names? Just a thought off the top of my head, I don't know how well/efficiently that would work

Also, you have no identifier for the int to be passed into printName(). And why are you passing an int which you are not utilizing anyway?

Something like this, I suppose:

1
2
3
4
5
6
7
8
9
10
11
12
13
void printName(char name[], unsigned int size){//for size of array
        bool space = false;
        string first = "", last = "";
	for (int count = 0; count < size; count ++){
		if(isspace(name[count]))
                       space = true;
                else if(space == false)
                       first += name[count];
                else if(space == true)
                       last += name[count];
}
//then print the names
}
Last edited on
hey, thanks for your reply Captian Cosine. i appreciate it.
still working on this one. driving me nuts.
i like your ideas.
here's what i have so far.
it's close, but it's getting awfully confusing...

1
2
3
4
5
6
7
8
9
10
int count, index, num, digit, x, y;

	for (count = 0; name[count] != ' '; count ++); //loops to find space at end of first name
	for (index = count; name[index] != '\0'; index++); // loops to find \0 at end of last name
	
	for (digit = 0; digit < index - count; digit++)// FIRST NAME
			reversed[digit] = name[count+1];

	for (num = digit + index - count; num < index; num ++)// LAST NAME
			reversed[index-count] = name[digit];
Topic archived. No new replies allowed.