Page 1 of 1

string manipulation

Posted: Wed Jul 24, 2002 11:00 pm
by ubershatten
Hi experts,

I am fairly new in the C++ world and I am trying to manipulate a string.  This is for a class.  I have to read text one character at a time (not suppose to use arrays yet) then swap every other character.  Example if user typed in "mouse" the output would be "omsue." Thank you for your help.  

RE:string manipulation

Posted: Wed Jul 24, 2002 11:00 pm
by little_endian
<pre>
//by [email protected]
#include <iostream>
#include <string>

using std::cout;
using std::string;

public class EveryOtherCharInAStringSwapper{

public:
  string swapEveryOtherChar(String str){
    string ret;
    
    ret.resize(str.size());
    for(int i=0;i<str.size()-1;i+=2){
      ret[i] = str[i+1];
      ret[i+1] = str[i];
    }
    if(i == str.size()-1){
      ret[i] = str[i];
    }
    return ret;

  }

}



</pre>

RE:string manipulation

Posted: Thu Jul 25, 2002 11:00 pm
by ubershatten
I am kind of confused by your source code:
this is what I have so far:

#include <iostream.h>
#include <cctype>

void main()
{
   char *name;
   const int MAX = 99;
   char input[MAX+1];
   cin.getline(input, MAX);
   char string;
   name = input;

   //prompting user to enter word
   cout << "Please enter the word to be manipulated: " << endl;
   cin >> name;

   //switching any uppercase to lowercase
   for(int x =0; x < MAX; x++)
      name[x] = tolower(name[x]);

   //switching the word backwards
   cout << "The word backwards is: ";
   for(int x = strlen(name) -1; x >=0; x--)
      cout << *(name + x) << endl;
  
   //trying to swap every other character
   cout <<"After swapping every other character the word is: ";
   for(int x = 0; x < strlen(name) +1; x += 2)
      cout << *(name + x) << endl;



-------------------------------------------------------------------------------
The problem I am having is that the output of  "After swapping every other character the word is."  lets say the user entered the word mouse.....the output then is mue.  As you can see I do understand how to read every other character but I don't undersand how to swap it. So then the output is omsue not just mue.  If you can help I would appreciate it.


  
  

RE:string manipulation

Posted: Fri Jul 26, 2002 11:00 pm
by ubershatten
Never mind little_endian I figured it out.
My code is now this and it works fine.

   #include <cstring>
   #include <iostream.h>
   #include <cctype>   //for tolower

   int main()
{
    const int MAX = 99;
    char input[MAX+1];
    char string;
  
    //Prompt the user to enter the word
    cout << "Please enter the word to be manipulated: " << endl;
    cin.getline(input, MAX);
  
    //Turn all uppercase into lowercase
    for(int x = 0; x < MAX; ++x)
    input[x] = tolower(input[x]);

    //Switching word backwards
    cout << "The word you entered spelled backward is: ";
    for(int x = strlen(input) - 1; x >= 0; --x)
        cout << input[x];
    cout << endl;
    
    //Swapping every other character
    const int length = (strlen(input) / 2) * 3;
    
    cout << “After swapping every other character the word is: “;
    for(int x = 0; x < length; x += 2)
        cout << input[x+1] << input[x];
    cout << endl;

}

---------------------------------------------------------------------
I do want to thank-you for your help