C++ program to revere sentence
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: C++ program to revere sentence

  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
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,308

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

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

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center