Backward String
Write a function that accepts a C-string as an argument and displays its contents backwards. For instance, if the string argument is “Gravity” the function should display “ytivarG”. Demonstrate the function in a program that asks the user to input a string and then passes it to the function.
Here are the error messages I cannot get rid of:
1>------ Build started: Project: DayOfYear, Configuration: Debug Win32 ------
1> DayOfYear.cpp
1>c:\users\kim\documents\visual studio 2010\projects\dayofyear\dayofyear\dayofyear.cpp(125): fatal error C1075: end of file found before the left brace '{' at 'c:\users\kim\documents\visual studio 2010\projects\dayofyear\dayofyear\dayofyear.cpp(29)' was matched
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Here is my code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstring>
// Not sure if I need all libraries but it can't hurt. :)/>
using namespace std;
int main()
{
// declare string variable to accept user input
string word;
// ask for user to type a word
cout << "Please enter a word and the magical computer will print it backwards: " << word << endl;
// C-String handling function
void displayString(char str[] )
{
// define variable to use as index
int index = 0;
// step through string until null character is reached
while ( str[index] != '\0' )
{
cout << str[index];
index++;
declare variable to count the letters
//int letterCount;
// measure length of string entered
// set idex of memory locations of letters in typed string equal to letterCount
while ( letterCount != 0; letterCount = index; letterCount--; )
{
// create a string to print out each letter as we loop through string length measurement
string letter;
// print the letter
cout << letter << endl;
} // end while
} // end step through
// end lthe line
cout << endl;
} // end C-String handling function
system("Pause");
return 0;
}
Sorry, posted wrong error messages. Here are the correct ones:
1>------ Build started: Project: BackwardString, Configuration: Debug Win32 ------
1> Backward String.cpp
1>c:\users\kim\documents\visual studio 2010\projects\backwardstring\backwardstring\backward string.cpp(30): error C2601: 'displayString' : local function definitions are illegal
1> c:\users\kim\documents\visual studio 2010\projects\backwardstring\backwardstring\backward string.cpp(19): this line contains a '{' which has not yet been matched
1>c:\users\kim\documents\visual studio 2010\projects\backwardstring\backwardstring\backward string.cpp(45): error C2143: syntax error : missing ')' before ';'
1>c:\users\kim\documents\visual studio 2010\projects\backwardstring\backwardstring\backward string.cpp(45): error C2059: syntax error : ')'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

New Topic/Question
Reply




MultiQuote






|