Exchange 1st & last letter in string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kobe67
    New Member
    • Dec 2009
    • 1

    Exchange 1st & last letter in string

    Create a program that will ask a string from the user, your program should automatically exchange the 1st & the last letter of every word in the string
  • jabbah
    New Member
    • Nov 2007
    • 63

    #2
    have you read this


    and do you have a question?

    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      One way of doing this is to initialize a string with a sentence and allocate pointers to the 1st and last letters of the first word.
      Code:
      string s ="When will you be back";
      *p1=&s[0];
      *p2=&s[3];
      swap(p1,p2) // prints ' nheW'
      Now use a for loop to change the remaining four words with the following within its body.
      p2+=2; //points to the 1st letter in next word
      p1=p2; // p1 does the same and is where you want it
      Then advance p2 to the last letter in 2nd word by:
      while(p2>='a' && p2<='z') p2++;//it will go too far (to the space) so p2- - outside the while loop.
      now swap(*p1,*p2);

      Comment

      • solita
        New Member
        • Jun 2009
        • 17

        #4
        try to use strtok to locate the words in a sentence and then swap the first letter and last letter of this word.

        Comment

        • whodgson
          Contributor
          • Jan 2007
          • 542

          #5
          Nope. Unless you know how to grab and work on each individual token and then reverse tokens back to a string.

          Comment

          Working...