Simple Coding Challenge

The Partridge Family were neither partridges nor a family. Discuss.
User avatar
Alacaster
Posts: 81
Joined: October 16th, 2016, 5:08 pm
Location: Spokane, WA

Simple Coding Challenge

Post by Alacaster » March 5th, 2018, 2:49 am

Create a program that takes in an input of characters and outputs the same character array without redundant spaces. Such that

Code: Select all

"I     really  like muffins          they are good."
comes out as

Code: Select all

"I really like muffins they are good."
Do it so that your code complies to the smallest file size possible. Post your code below for all to see!

Mine will be posted in a few days or less. Have fun!

Edit: The forum does this automatically, oops!

Edited by Albinopapa for clarity.
You can't be betrayed if you don't have any friends.
Why live? Cause why not.

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Simple Coding Challenge

Post by albinopapa » March 5th, 2018, 3:52 am

Yeah, you need to put the example in a

Code: Select all

[code]
[/code] block.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
Alacaster
Posts: 81
Joined: October 16th, 2016, 5:08 pm
Location: Spokane, WA

Re: Simple Coding Challenge

Post by Alacaster » March 5th, 2018, 4:21 am

okay, thanks.
You can't be betrayed if you don't have any friends.
Why live? Cause why not.

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Simple Coding Challenge

Post by albinopapa » March 5th, 2018, 5:13 am

First try: C++
File size: 25,088 bytes

Code: Select all

#pragma once

#include <iostream>
#include <sstream>
#include <string>
#include <vector>

std::string getUserInput()
{
	std::cout << "Enter a string, I'll remove extra spaces. \n";

	std::string result;
	std::getline( std::cin, result );

	return result;
}

std::string formatString( const std::string& Input )
{
	std::string result;
	result.reserve( Input.size() );

	std::stringstream ss;
	ss << Input;

	std::string temp;
	for( ; ss.good(); ss >> temp )
	{
		if( !temp.empty() )
			result += ( temp + " " );
	}

	result += temp;
	return result;
}

int main( int argc, char* argv[] )
{
	const auto input = getUserInput();
	const auto result = formatString( input );
	std::cout << result << std::endl;

	return 0;
}
Second try: C/C++
File size: 9,216 bytes

Code: Select all

#pragma once

#include <cstdio>
#include <cctype>

constexpr size_t MAX_LENGTH = 512;
void getUserInput( char( &buffer )[ MAX_LENGTH ] )
{
	const char instruct[] = "Enter a string, I'll remove extra spaces. \n";
	printf( "%s", instruct );
	gets_s( buffer );
}

void formatString( const char( &inBuffer )[ MAX_LENGTH ], char( &outBuffer )[ MAX_LENGTH ] )
{
	char c = inBuffer[ 0 ];
	
	size_t out_pos = 0;
	for( size_t i = 0; i < MAX_LENGTH && c != '\0'; ++i, c = inBuffer[ i ] )
	{
		if( isalpha( c ) || ispunct( c ) )
		{
			outBuffer[ out_pos ] = c;
			++out_pos;
		}
		else if( isspace( c ) )
		{
			if( outBuffer[ out_pos - 1 ] != ' ' )
			{
				outBuffer[ out_pos ] = c;
				++out_pos;
			}
		}		
	}
}

int main( int argc, char* argv[] )
{
	char input[ MAX_LENGTH ]{};
	getUserInput( input );

	char output[ MAX_LENGTH ]{};
	formatString( input, output );
	
	printf( "%s", output );

	return 0;
}
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
Alacaster
Posts: 81
Joined: October 16th, 2016, 5:08 pm
Location: Spokane, WA

Re: Simple Coding Challenge

Post by Alacaster » March 5th, 2018, 6:01 am

Well, here is mine. In C.
8,704 bytes

Code: Select all

int main()
{
    int bb='\0';
    printf("Have a twitchy thumb? No Problem!\nType:");
    while((bb=getchar()) != 10){

        while(bb==' '){
            bb=getchar();

            if(bb!=' '){
                printf(" %c", bb);
                bb=getchar();
            }
        }
    }
    return 0;
}
Wow, you sure do know a lot of stuff Albinopapa. It gave me an error message when I tried to compile yours.
C:\Users\*******\Documents\Code Blocks\Buzzy Bucky\Test File\main.cpp:1:9: warning: #pragma once in main file
#pragma once
^
C:\Users\*******\Documents\Code Blocks\Buzzy Bucky\Test File\main.cpp: In function 'void getUserInput(char (&)[512])':
C:\Users\Gabriel\Documents\Code Blocks\Buzzy Bucky\Test File\main.cpp:11:19: error: 'gets_s' was not declared in this scope
gets_s( buffer );
^
You can't be betrayed if you don't have any friends.
Why live? Cause why not.

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Simple Coding Challenge

Post by albinopapa » March 5th, 2018, 7:56 am

Hmm, yeah, it wasn't very portable lol. I think gets_s is a Microsoft function that is "safer" than the C gets(). Also, so is the #pragma once, it too is a Microsoft Visual Studio thing. I hate using include guards
#ifdef SOMEFILE_H
#define SOMEFILE_H
// code
#endif

Honestly, don't know why it's in there, it was all in a CPP file lol.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Simple Coding Challenge

Post by albinopapa » March 5th, 2018, 8:12 am

Yours doesn't fulfill the requirements though.
outputs the same character array without redundant spaces
While typing, I was still able to insert multiple spaces, and it doesn't output the newly formatted string.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Simple Coding Challenge

Post by albinopapa » March 5th, 2018, 8:14 am

Just going to put this here. To run the C++ version, change UseCpp 0 to UseCpp 1

Code: Select all

#include <cstdlib>
#define UseCpp 0

#if UseCpp		
//  25,088 bytes x86 mode [opt_file_size = 23,552 bytes, 
//  31,232 bytes x64 mode [opt_file_size = 29,696 bytes
#include <iostream>
#include <sstream>
#include <string>

#else		// UseCpp 0
//	 9,216 bytes x86 mode [ opt_file_size =  9,216 bytes, 
//	11,776 bytes x64 mode [ opt_file_size = 11,776 bytes
#include <cstdio>
#include <cctype>

#define MAX_LENGTH 512

#endif

#if UseCpp
std::string getUserInput()
#else
void getUserInput( char( &buffer )[ MAX_LENGTH ] )
#endif
{
	constexpr const char* instruct = "Enter a string, I'll remove extra spaces. \n";
#if UseCpp
	std::cout << instruct;
	std::string result;
	std::getline( std::cin, result );

	return result;
#else
	//const char instruct[] = "Enter a string, I'll remove extra spaces. \n";
	printf( "%s", instruct );	
	fgets( buffer, MAX_LENGTH, stdin );
#endif
}

#if UseCpp
std::string formatString( const std::string& Input )
#else
void formatString( const char( &inBuffer )[ MAX_LENGTH ], char( &outBuffer )[ MAX_LENGTH ] )
#endif
{
#if UseCpp
	std::string result;
	result.reserve( Input.size() );

	std::stringstream ss;
	ss << Input;

	std::string temp;
	for( ; ss.good(); ss >> temp )
	{
		if( !temp.empty() )
			result += ( temp + " " );
	}

	result += temp;
	return result;
#else
	char c = inBuffer[ 0 ];

	size_t out_pos = 0;
	for( size_t i = 0; i < MAX_LENGTH && c != '\0'; ++i, c = inBuffer[ i ] )
	{
		if( isalpha( c ) || ispunct( c ) )
		{
			outBuffer[ out_pos ] = c;
			++out_pos;
		}
		else if( isspace( c ) )
		{
			if( outBuffer[ out_pos - 1 ] != ' ' )
			{
				outBuffer[ out_pos ] = c;
				++out_pos;
			}
		}
	}
#endif
}

int main( int argc, char* argv[] )
{
#if UseCpp
	const auto input = getUserInput();
	const auto result = formatString( input );
	std::cout << result << std::endl;
#else
	char input[ MAX_LENGTH ]{};
	getUserInput( input );

	char output[ MAX_LENGTH ]{};
	formatString( input, output );

	printf( "%s", output );
	
#endif

	system( "pause" );

	return 0;
}
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Simple Coding Challenge

Post by albinopapa » March 5th, 2018, 8:27 am

I don't know if this is a C thing or not
char (&buffer)[512]

It passes the array by value, so the compile time size is maintained. I learned that from cyboryxmen a few months ago.

The second code post I changed the constexpr to a #define and changed the gets_s to fgets and added the system("pause"). Still, in x86 mode it compiles to the same size.

Code: Select all

Have a twitchy thumb? No Problem!
Type:I     really  like muffins          they are good.
 r l m t a gPress any key to continue . . .
This is the result of running your code btw.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Simple Coding Challenge

Post by albinopapa » March 5th, 2018, 8:28 am

I compiled your code and checked the file size, it's still the same as mine at 9,216 when compiled with VS2017.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Post Reply