CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2014
    Posts
    2

    Question C++ program to revere sentence

    Hello everyone I had been tasked to create a program that reverses a word i.e.
    cat
    tac
    Can someone tell me how to make my program non case-sensitive using vectors?
    Code:
    // BackWardsSentence.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"
    #include <iostream>
    #include <sstream>
    #include <string>
    using namespace std;
    string BackWords (string sentence);
    int BackSentence (string sentence);
    string BackWords1 (string sentence);

    int _tmain(int argc, _TCHAR* argv[])
    {
    string sentence;
    int choice;
    cout<< "What is your sentence" << endl;
    getline (cin,sentence);
    cout<< "You entered" << " "<< sentence;
    cout<< " "<<"If you would like to reverse letters enter 0 if you would like to reverse words enter 1"<<endl;
    cin>> choice;
    if(choice==0)
    {
    cout<< "Your new sentence is " << " " <<BackWords(sentence)<< endl;
    }
    if(choice==1)
    {
    cout<< "Your new sentence is" <<" "<<BackSentence(sentence)<<endl;
    }
    return 0;
    }

    string BackWords (string sentence)
    {
    int length= sentence.length(); //3
    int x=0;
    int y=length-1; //2
    int a=0;
    while (x<length-1) //x<3
    {
    string sv;
    string sb;
    string sy;
    char const v=sentence.at(x);
    char const b=sentence.at(y);
    sv = (v);
    sb = (b);
    if(x==0)
    {
    sentence.replace(x,1,sb);
    sentence.replace(y,1,sv);
    }
    if (x==1)
    {
    sentence.replace(x,1,sb);
    sentence.replace(y,1,sv);
    }
    x++;
    y--;
    }
    return sentence;
    }

    /*string BackWords1 (string sentence) {
    int start = 0;
    int end = sentence.length() - 1;
    while (start < end) {
    char temp = sentence[end];
    sentence[end] = sentence[start];
    sentence[start] = temp;
    ++start;
    --end;
    }
    return sentence;
    }*/


    int BackSentence (string sentence)
    {
    int length = sentence.length();

    return length;
    }

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: C++ program to revere sentence

    Please use code tags when posting code. As posted, your code is very hard to read. Go advanced, select the formatted code and click '#'. What's the issue with the code you have posted?

    I'm not sure I understand what you mean by non-case sensitive in this context? Do you want the reversed word to be just lowercase if it contains a mixture of upper and lower case? tolower() [or toupper()] will return the lower-case [or upper case] of the supplied char. See http://msdn.microsoft.com/en-us/library/8h19t214.aspx

    Why do you want to use vectors for reversing characters in a string? Is this an assignment? Have a look at string iterators and reverse iterators http://www.cplusplus.com/reference/string/string/
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: C++ program to revere sentence

    hello, you don't need to write any program. use command tac -r --separator="." file.txt

    http://sourceforge.net/projects/unxutils/
    Nobody cares how it works as long as it works

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured