In my class we had to be able to store chars, ints, and strings inside of both a string and an array. Then switch around or shuffle the positions of the data stored. I was able to write programs to do it for both the int and char data type but I'm completely lost on how to do it for the string data type. Here's what I what I had what I had for the char data type. How would I do the same thing but for strings??
cout << "Please enter 5 characters to store in a string and array" << endl;
for (int i=0; i < 5; i++) {
cin >> currentchar;
storage1 = storage1 + currentchar;
storage2[i] = currentchar;
}
store = storage1[0];
storage1[0] = storage1[4];
storage1[4] = store;
Lol the name of that array was pretty funny... but thank you for your help I appreciate it. The program is doing what i want for the most part and I can switch around words in the array but how would you switch around the words in the string.
1. Begin a simple program that will ask the user for some different types of input. We’ll be
storing this input in both a string and an array. Remember to include the string header file
to get all of the string capabilities.
2. We’ll start by asking for char input. Declare a string and an array to store the input. One of
the major differences between strings and arrays is that you can always append information
onto a string using concatenation or the + operator, BUT arrays have fixed storage space.
So you’ll have to initialize a size for the array and let the user know there’s a limit to the
number of characters they can store.
3. Iteratively ask the user to enter data, which will then be stored both in your string and your
array. One straightforward way to go about this is storing the input into a char variable and
then appending the information to the string, and initializing the appropriate storage space
in the array using the [ ] operator.
4. Attempt to shuffle the information stored. IE, try to switch the information stored in the
first position of the string and array with the information stored in the third position and
vice versa. Is switching one storage method more straightforward and if so why?
5. Repeat the above steps twice, first using integers instead of chars and then using strings.
The guidelines appear to be written by someone who doesn't expect to be understood, so I can only guess. :(
It seems the exercise revolves around strings vs c-style strings rather than what I addressed above.
If I am correct then you need to checkout the functionality of each data type. Each has a large number of functions you can draw on to manipulate each:
a) c-string ( an array of characters ) http://www.cplusplus.com/reference/cstring/
b) string class http://www.cplusplus.com/reference/string/
It's worth spending a bit of time surfing through these functions. There are plenty of demo examples.