Need some help with "C-String"/Arrays

closed account (3bXy6Up4)
So this is my first post here so forgive me if I'm doing it wrong but essentially I have to write a loop assigning a variable x to all positions of a string variable and I'm stuck. I don't have extensive experience with arrays and I'm also a bit confused about C-String. The problem is below. Any help at all is greatly appreciated!

"Given the following declaration and initialization of the string variable, write a loop to assign 'X' to all positions of this string variable, keeping the length the same.

char our_string[15] = "Hi there!";

(Please note this is a 'C-string', not C++ standard string.)"
Last edited on
So what exactly is the problem? All you've done is posted the question you need to answer, but we don't know what part of it you don't understand or are having trouble with.
closed account (3bXy6Up4)
Sorry about that. I don't want anyone to write my program for me, I guess I just don't even know where to begin. I've never combined loops and arrays before. Would I begin with a "for" loop? This seems like the best option but I'm not sure which direction to go in. Any general guidance would help.
If you don't even know where to begin maybe you'd be better served going over some of your previous material to see what options are available to you. I'd agree using a loop is a good choice, since you're going to need to check each character in the string. Do you have any idea (pseudocode is a good technique) of a general strategy to look at each character?
I understand your question, and I know how to solve it. I'll see if I can lead you in the right direction.

When initializing arrays, the name of the array is a constant pointer to the memory address of the array's first element. For example:
1
2
char our_string[15] = "Hi there!"; // our_string acts as a pointer to 'H' in "Hi there"
                                   // thus, our_string holds the address of 'H' 

So, that being said, think of the variable, our_string, simply as the [0] index. Literally. For example:
1
2
3
std::cout << *our_string       << std::endl; // this will print out the value 'H'
std::cout << out_string[0]     << std::endl; // this will also print out the value 'H'
std::cout << *(out_string + 0) << std::endl; // as will this 


I hope this will help you understand a little of what's going on and what the question of trying to ask you to do.
Last edited on
Isn't the assignment just asking you to replace all the characters with 'X' ?
Topic archived. No new replies allowed.