By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,564 Members | 834 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,564 IT Pros & Developers. It's quick & easy.

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

100+
P: 542
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.

Share this Question
Share on Google+
2 Replies


100+
P: 687
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

100+
P: 542
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

Post your reply

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