Hello, this is my first time posting to these forums but they have been extremely helpful throughout my college semester. I have a program here that I have been working on for a week now and the criteria to it is very specific.
I have written and been able to get it to work but without following the criteria.
I could turn it in as is but I would really like to know this concept.
What I have to do is write a function that takes only 1 parameter and print it using pointers. The part I am having difficulty doing is keeping the cout out of the function itself.
#include <iostream>
#include<cstring>
#include<string>
usingnamespace std;
void stringReverse(char *CString);
int main()
{system("title _ Reverse String _");
system("color 0c");
char str[100];
cout << "you can input a string of characters or a sentence and I will reverse it for you" << endl << endl;
cin.getline(str, 99);
cout << endl;
cout << " your sentence or string in reverse is: " << endl << endl << endl;
stringReverse(str); // string reversed function call
cout << str << endl; // srting reprinted as is (forward)
cout << endl;
system ("PAUSE");
return 0;
}
void stringReverse(char *CString) //changed this in class
{
int len = strlen(CString);
char *p = &CString[ len - 1];
for(int i = 0 ;i < len; i++)
*p--; // minus, minus , instead of plus , plus, for reverse (decrement) (-- ! ++)
}
ever since i took the cout out of the function it will not reverse the string anymore. Can anyone help?
#include <iostream>
#include<cstring>
usingnamespace std;
void stringReverse(char *);
int main(void)
{
char str[100];
cout << "you can input a string of characters or a sentence and I will reverse it for you" << endl;
cin.getline(str, 99);
cout << endl;
cout << "your sentence or string in reverse is: " << endl;
stringReverse(str); // string reversed function call
cout << str << endl; // srting reprinted as is (forward)
return 0;
}
void stringReverse(char *cstr)
{
int len = strlen(cstr);
char swap;
for (int i = 0; i < len/2; i++)
{
swap = cstr[i];
cstr[i] = cstr[len-i-1];
cstr[len-i-1] = swap;
}
}
This is exactly what needs to happen with the program but I have to specifically use a single parameter within the function called CString. I tried switching cstr out from your example but it would not run since CString does not seem to be initializing.
the idea is to learn how to use the '\0' null character that comes with the CString array. That is the part that I do not understand. . .