Daily Programming Challenge #1 22/02/12
2 posters
RUSES :: Programming :: Challenges
Page 1 of 1
Daily Programming Challenge #1 22/02/12
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++
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- Posts : 67
Join date : 2012-02-23
Location : Come at me bro
Re: Daily Programming Challenge #1 22/02/12
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.
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).
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:
- Code:
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
void separate(char *);
void sort(char *, int );
char in[100], vOut[100], cOut[100];
int main()
{
cout << "Enter a word: ";
cin.getline(in, 100);
separate(in);
sort(vOut, 1);
sort(cOut, 0);
cout << endl << "Vowels: " << vOut << endl;
cout << "Consonants: " << cOut << endl << endl;
system("PAUSE");
system("cls");
main();
}
void separate(char * input)
{
int vCount = 0, cCount = 0, length = strlen(input);
for(int i = 0; i < length; i++)
{
if( input[i] == 'a' || input[i] == 'A' ||
input[i] == 'e' || input[i] == 'E' ||
input[i] == 'i' || input[i] == 'I' ||
input[i] == 'o' || input[i] == 'O' ||
input[i] == 'u' || input[i] == 'U' )
{
vOut[vCount] = tolower(input[i]);
vCount++;
}
else if(isalpha(input[i]) == 0)
{
// ignore characters that aren't letters
}
else
{
cOut[cCount] = tolower(input[i]);
cCount++;
}
}
}
void sort(char * input, int isVowel)
{
int length = strlen(input);
for(int i = 0; i < length; i++)
{
for(int j = 0; j < length-1; j++)
{
if(input[j + 1] < input[j])
{
char temp = input[j + 1];
input[j + 1] = input[j];
input[j] = temp;
}
}
}
if(isVowel == 1)
{
for(int i = 0; i < length; i++)
{
vOut[i] = input[i];
}
}
else
{
for(int i = 0; i < length; i++)
{
cOut[i] = input[i];
}
}
}
Charlie- Posts : 16
Join date : 2012-02-23
Re: Daily Programming Challenge #1 22/02/12
Yep that works +1 point!
Tell me if you want (emotionless) constructive criticism - read rules for more info
Tell me if you want (emotionless) constructive criticism - read rules for more info
Alex- Posts : 67
Join date : 2012-02-23
Location : Come at me bro
Re: Daily Programming Challenge #1 22/02/12
- 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
Alex- Posts : 67
Join date : 2012-02-23
Location : Come at me bro
Re: Daily Programming Challenge #1 22/02/12
I did that so it would sort the vowels and the consonants individually but otherwise fair points ^^AlexDiru wrote:
- the sort function is called twice, thus increasing the algorithm time by two
Charlie- Posts : 16
Join date : 2012-02-23
Similar topics
» Daily Programming Challenge #2 23/02/12
» Weekend Programming Challenge #3 24/02
» Programming Challenge #4 - Code Obfuscation
» Challenge Points
» Challenge Rules
» Weekend Programming Challenge #3 24/02
» Programming Challenge #4 - Code Obfuscation
» Challenge Points
» Challenge Rules
RUSES :: Programming :: Challenges
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum