473,387 Members | 1,863 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,387 software developers and data experts.

First word not being processed correctly.......not urgent.

542 512MB
The following code accepts a string from the user and swaps the first and last letters in each word. It prints out the original string incorrectly by dropping off the first letter of the first word. I would like to establish what error in the code is causing the first word to be mangled.
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdlib>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. cout<<"Transposing letters in a string \n";
  9. cout<<"Type a short sentence (end with Ctrl+Z)"<<endl;
  10. char s[81]={0};
  11. int count=0;
  12. do
  13. {
  14. cin.getline(s,81);
  15. //if(*s)cout<<s;  //this prints the sentence correctly
  16. }while(*s);
  17.  
  18. for(int i=0;i<sizeof(s);i++)        
  19.   cout<<s[i];
  20.   if(s[i]==' ')count++;
  21. cout<<"\nThere are "<<count<<" words in the sentence\n";
  22.  
  23. char *p1=&s[0];          //set both pointers to 1st letter
  24. char *p2=&s[0];
  25. while(*p2>='a' && *p2<='z' || *p2>='A' && *p2<='Z' )
  26.        { p2++; }         // advance p2 to end of 1st word
  27.         if(*p2==' ')
  28.         p2--;  
  29.        swap(*p1,*p2);
  30.  
  31.     for(int i=0;i<count;i++)//remaining  words 
  32.      { p2+=2;           //position p2 on 1st letter of next word
  33.        p1=p2 ;          // move p1 to same position
  34.        while(*p2>='a' && *p2<='z' && *p2!='\n')
  35.        { p2++;  }       // advance p2 to end of next word
  36.        if(*p2==' ')p2--;//if it goes to space..bring back one
  37.        swap(*p1,*p2);        
  38.  
  39.     }
  40. cout<<"After swapping, the string becomes: \n";   
  41.  
  42. for(int i=0;i<sizeof(s);i++)        
  43. {  
  44.   cout<<s[i]; 
  45. }   
  46. cout<<"\n\n";
  47. char q;    
  48. cout<<"Press any key to continue...";
  49. cin>>q;
  50.  
  51. return 0;
  52. }
/*
Transposing letters in a string
Type a short sentence (end with Ctrl+Z)
Swapping the first and last letter in each word ^Z

wapping the first and last letter in each word
There are 9 words in the sentence
After swapping the string becomes:
wgppina eht tirsf dna tasl rettel ni hace dorw

Press any key to continue...
*/
Dec 22 '09 #1

✓ answered by newb16

When you type ^Z and press enter, you while loop is terminated and 0 is stored at the first element of s, overwriting the first letter.

2 1817
newb16
687 512MB
When you type ^Z and press enter, you while loop is terminated and 0 is stored at the first element of s, overwriting the first letter.
Dec 22 '09 #2
whodgson
542 512MB
Many thanks........I looked at about 15 possible causes but that wasn`t one of them.....although i did notice that the space between the first and second word was sometimes wider. Anyway I can now do something about it.....thanks again newb16.
Dec 23 '09 #3

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

Similar topics

6
by: Peter Kleiweg | last post by:
I'm still new to Python. All my experience with OO programming is in a distant past with C++. Now I have written my first class in Python. The class behaves exactly as I want, but I would like to...
11
by: Ron | last post by:
Hello, I'm having an aggravating time getting the "html" spewed by Word 2003 to display correctly in a webpage. The situation here is that the people creating the documents only know Word, and...
14
by: g | last post by:
http://mysite.verizon.net/gdguarino/montauk.htm You can also click on the other pages of the site if the problem doesn't show itself on this page. I'm using two stylesheets for each page, one...
3
by: Phil Rutter | last post by:
Hello All, I have about 700 word documents that have 2 tables one is static 4 colums x 5 rows the other is 5 colums x rows ranging from 2 to 100 what i wolud like to do is open the word doc....
14
by: TS | last post by:
I have this custom data list control and i override the onItemDatabound event. After upgrading to vs 2005, this event is not always getting called, though it does at other times. No changes were...
4
by: dragooon | last post by:
Does anybody notice that word document does not work with frameset? If you load a word document into a frame, this frame then can not be resized, but if you load other office documents it works...
15
by: robert maas, see http://tinyurl.com/uh3t | last post by:
Here's the source: #include <stdio.h> #include <errno.h> main () { char* str = "9999999999"; long long int llin; char* endptr; /* Set by strtoll */ int nch; errno = 0; llin = strtoll(str,...
2
by: Derek Hart | last post by:
I am using late bound Microsoft Word integration with a vb.net winforms application. If I run code such as the following: Dim objWord As Object Dim objWrdDoc As Object Dim count As Integer...
2
by: JeffN825 | last post by:
Hello, Thank you in advance for any assistance. I am trying to set up a mail merge from VB6/VBA code with Word 2000 using an ODBC data source (SQL 2000). I have successfully gotten the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.