473,385 Members | 1,593 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How can I swap the first and last letters of the input stream in this program?

Hi, this is part of an assignment I was given in my introductory programming course. I simply need to know how I could swap the first and last letters of the input in this program:

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     cout << "---------------------------------------------------------" << endl;
  4.     cout << "              Letter Swapping Program" << endl;
  5.     cout << "---------------------------------------------------------" << endl;
  6.  
  7.     string word;
  8.     cout << "Please enter a word at least 3 letters long: ";
  9.     cin >> word;
  10.     cout << endl << endl;
  11.  
  12.     int wordLen = word.length();
  13.     char firstLet = word.at(0);
  14.     char lastLet = word.back();
  15.  
  16.     cout << "The length of " << word << "is " << wordLen << endl;
  17.     cout << "The first and last letters of " << word << " are " << firstLet << " and " << lastLet << " respectively." << endl;
  18.     cout << "If you swap the first and last letters of this word, you get: ";
  19.  
By all means, I know this is a messy program and is not the most concise way to write it, but that's the extent of my knowledge so far in this class.

Thanks!
Sep 22 '14 #1
1 3069
weaknessforcats
9,208 Expert Mod 8TB
To swap, move one character to a temp variable while you load the other:

temp var = first letter;
first letter = last letter;
last letter = temp;

I'm trying to avoid writing the exact code so you can leanr this yourself.
Sep 22 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: Jason Heyes | last post by:
I want to allow objects of my class to be read from an input stream. I am having trouble with the implementation. Here are the different approaches I have tried: // Version 1.0 - Default...
2
by: Sonny | last post by:
Hi experts, I am writing a small code to read input from a file, for example my input file is: X Y Z 26.0 28.0 0.0 32.0 8.0 0.0 My code is:...
8
by: brian.digipimp | last post by:
I turned this in for my programming fundamentals class for our second exam. I am a c++ newb, this is my first class I've taken. I got a good grade on this project I'm just wondering if there is a...
9
by: kernelxu | last post by:
hi,everybody. I calling function setbuf() to change the characteristic of standsrd input buffer. some fragment of the progrem is: (DEV-C++2.9.9.2) #include <stdio.h> #include <stdlib.h> int...
16
by: lovecreatesbeauty | last post by:
/* When should we worry about the unwanted chars in input stream? Can we predicate this kind of behavior and prevent it before debugging and testing? What's the guideline for dealing with it? ...
3
by: Nobody | last post by:
I'm trying to put together code to deal with a SOAP with attachements response, and I'd like to process the response in a single pass. The SOAP with attachments specification returns XML in a MIME...
0
by: lini | last post by:
Hello, I am writing some code in the scenario which can be described as follow: + program A which writes to standard output (e.g. cout >> whatever). + program B which has GUI and also listens to...
4
by: pms | last post by:
Need suggestions to improve this program. using System; namespace CalculateWage { class GetEmployeeDetails {
82
by: Bill David | last post by:
SUBJECT: How to make this program more efficient? In my program, a thread will check update from server periodically and generate a stl::map for other part of this program to read data from....
27
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
I have a fully-portable C program (or at least I think I do). It works fine on Windows, but malfunctions on Linux. I suspect that there's something I don't know about the standard input stream...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.