RUSES
Would you like to react to this message? Create an account in a few clicks or log in to continue.
May 2024
MonTueWedThuFriSatSun
  12345
6789101112
13141516171819
20212223242526
2728293031  

Calendar Calendar


Daily Programming Challenge #1 22/02/12

2 posters

Go down

Daily Programming Challenge #1 22/02/12 Empty Daily Programming Challenge #1 22/02/12

Post  Alex Thu Feb 23, 2012 9:58 am

POINTS NOW CLOSED

Create a function that returns 2 strings (i.e. return a string and have a pass by reference on the other OR return a struct containing 2 strings)

The function must take a string as input, and remove all the vowels from the input string.

The vowels and string must both be sorted and returned from the function.

i.e. Input -> "alexander"
Output -> "dlnrx" & "aaee"

Input -> "trees"
Output -> "rst" & "ee"

Solution C++
Code:
#include <iostream>
#include <string>

using namespace std;

inline bool isVowel(char);
string vowelSorter(const string&, string&);

int main()
{
   string userInput = "";

   cout << "> ";
   getline(cin,userInput);

   while (userInput != "exit")
   {
      string vowels,consonants;

      consonants = vowelSorter(userInput,vowels);
   
      cout << "Input: \t\t" << userInput << endl;
      cout << "Consonants: \t" << consonants << endl;
      cout << "Vowels: \t" << vowels << endl;

      cout << "> ";
      getline(cin,userInput);
   }

   return 0;
}

inline bool isVowel( char n ) //determines if a given character is a vowel or consonant
{
   n = tolower(n); //convert n to lower case

   switch (n)
   {
   //if n is a vowel
   case 'a':case 'e':case 'i':case 'o':case 'u':
      return true;
      break;
   //otherwise n must be a consonant
   default:
      return false;
   }
}

//the str is the input string
//returns the string of sorted CONSONANTS, the vowelString parameter is the where the vowel string is 'placed'
string vowelSorter ( const string& str, string& vowelString )
{
   string conString = ""; //the string of Consonants that will be returned
   vowelString.erase(); //empty the vowel string in case it already has characters in it

   for (int i=0;i<str.length();i++) //loop through each character of the input string
      if (isVowel(str[i])) //if the character is a vowel
         vowelString.push_back(str[i]); //add it to the vowel string
      else
         conString.push_back(str[i]); //add it to the consonant string

   //now we have both trees they must be sorted
   //for this i'll do a simple bubble sort
   //i could #include <algorithm> and call sort(vowelString) and sort(conString)
   //but that feels like cheating :P
   
   //sort vowelString
   for (int a=0;a<vowelString.length();a++)
      for (int b=0;b<vowelString.length();b++)
         if (vowelString[a]<vowelString[b])
            swap(vowelString[a],vowelString[b]);

   //sort consonant string
   for (int a=0;a<conString.length();a++)
      for (int b=0;b<conString.length();b++)
         if (conString[a]<conString[b])
            swap(conString[a],conString[b]);
   
   return conString;
}


Last edited by AlexDiru on Fri Feb 24, 2012 5:08 am; edited 1 time in total
Alex
Alex

Posts : 67
Join date : 2012-02-23
Location : Come at me bro

Back to top Go down

Daily Programming Challenge #1 22/02/12 Empty Re: Daily Programming Challenge #1 22/02/12

Post  Charlie Fri Feb 24, 2012 12:08 am

Mine's fairly simple, although I confused myself with pointers at first... It will accept an input of up to 100 characters, including spaces, numbers, punctuation etc. but it will ignore anything that isn't a letter. Haven't commented much, let me know if I should next time. Smile

Figured I'd put it in a spoiler, but I also put it on pastebin last night so it's here too: http://pastebin.com/eHwmi2Qb
(Personally I think it's a little easier to read when it's not all one colour).

Spoiler:
Charlie
Charlie

Posts : 16
Join date : 2012-02-23

Back to top Go down

Daily Programming Challenge #1 22/02/12 Empty Re: Daily Programming Challenge #1 22/02/12

Post  Alex Fri Feb 24, 2012 12:11 am

Yep that works Smile +1 point!

Tell me if you want (emotionless) constructive criticism - read rules for more info
Alex
Alex

Posts : 67
Join date : 2012-02-23
Location : Come at me bro

Back to top Go down

Daily Programming Challenge #1 22/02/12 Empty Re: Daily Programming Challenge #1 22/02/12

Post  Charlie Fri Feb 24, 2012 12:13 am

Yeah sure Smile
Charlie
Charlie

Posts : 16
Join date : 2012-02-23

Back to top Go down

Daily Programming Challenge #1 22/02/12 Empty Re: Daily Programming Challenge #1 22/02/12

Post  Alex Fri Feb 24, 2012 12:18 am

  • system() commands are OS specific
  • the sort function is called twice, thus increasing the algorithm time by two
  • no return int from the main function
  • char = tolower(char) could be called on the characters when they are being checked to see if they are a vowel thus reducing the comparisons by 5


As you can see I'm picky and too lazy to pick out what's good - it works so you know it's good Razz
Alex
Alex

Posts : 67
Join date : 2012-02-23
Location : Come at me bro

Back to top Go down

Daily Programming Challenge #1 22/02/12 Empty Re: Daily Programming Challenge #1 22/02/12

Post  Charlie Fri Feb 24, 2012 12:27 am

AlexDiru wrote:
  • the sort function is called twice, thus increasing the algorithm time by two
I did that so it would sort the vowels and the consonants individually but otherwise fair points ^^
Charlie
Charlie

Posts : 16
Join date : 2012-02-23

Back to top Go down

Daily Programming Challenge #1 22/02/12 Empty Re: Daily Programming Challenge #1 22/02/12

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum