473,729 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

542 Contributor
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
2 1846
newb16
687 Contributor
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 Contributor
Many thanks........I looked at about 15 possible causes but that wasn`t one of them.....althou gh 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
2312
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 get comments about coding style. I'm especially unsure about how a class should be documented, what to put in, and where. When to use double quotes, and when single. For instance, the doc string at the top must be in double quotes, or else the...
11
7038
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 aren't very computer savvy. I created a system where they can save their Word documents as "html" and upload them to a certain directory, and the web page dynamically runs them through tidylib using the tidy extension to php4, thus causing the...
14
2066
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 with the position and size information for each element and another for the color and background info. The position stylesheet is common to all pages. Sometimes only the "color" stylesheet loads, leaving the page a mess.
3
4312
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. import the first word table then import the second word table close word doc open next word doc and repeat process. i am able to import one set of data currently into an excel spread sheet but how can i loop through all the documents and import them...
14
8681
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 made when upgrading to 2.0 is there any reason for this because of .net 2.0? thanks
4
1295
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 just fine. Is there any workaround? Thanks!
15
9037
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, &endptr, 10); printf("errno=%d\n", errno);
2
4882
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 Dim filename As String filename = "c:\temp\mergedata\1.doc" objWord = CreateObject("Word.Application") For count = 1 To 10000
2
2156
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 connection working using both a file dsn and a machine dsn. I am currently using a simple test merge template with fields <<FirstName>> (a nvarchar) <<Id>> (an int primary key).....plus a <<NextRecord>> field. The template doc itself has no data source...
0
8917
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8761
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8148
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2163
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.