Simple Coding Challenge

The Partridge Family were neither partridges nor a family. Discuss.
nG Inverse
Posts: 115
Joined: April 27th, 2012, 11:49 pm

Re: Simple Coding Challenge

Post by nG Inverse » March 5th, 2018, 7:16 pm

Attempt taking albinopapa's approach

Code: Select all

#include <stdio.h>

int main()
{
	constexpr int size = 100;
	char input[size];
	char tmp[size];
	int index = 0;

	gets_s(input, sizeof(input));

	for (int i = 0; i < sizeof(input); i++)
	{
		if (input[i] == ' ')
		{
			if (i != 0 && input[i - 1] != ' ')
				tmp[index++] = input[i];
		}
		else tmp[index++] = input[i];
	}

	printf("\n%s",tmp);
	getchar();
	return 0;
}
And Alcaster's approach...

Code: Select all

#include <stdio.h>

int main()
{
	char c;
	bool space;

	while ((c = getchar()) != 10)
	{
		if (c == ' ')
		{
			if(!space)
				printf("%c", c), space = true;
		}
		else printf("%c", c), space = false;
	}
	getchar();
	return 0;
}
Both file sizes are the same (9,216) with VS2017.

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

Re: Simple Coding Challenge

Post by Alacaster » March 6th, 2018, 6:54 am

I fixed it.

Code: Select all

#include <stdio.h>
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();
            }
        }
        putchar(bb);
    }
    return 0;
}
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 6th, 2018, 9:18 am

It appears you have.

Change of subject, I didn't realize C++ code would bloat the size of the executable so much. I can see why C is still used in embedded system.
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

Slidy
Posts: 80
Joined: September 9th, 2017, 1:19 pm

Re: Simple Coding Challenge

Post by Slidy » March 6th, 2018, 12:10 pm

I think this would be more interesting as a sort of "code golf" challenge.
Do it in the least amount of characters you can :D

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

Re: Simple Coding Challenge

Post by albinopapa » March 6th, 2018, 6:44 pm

Slidy wrote:I think this would be more interesting as a sort of "code golf" challenge.
Do it in the least amount of characters you can :D
Alecaster's code comes in around 246 characters if you remove all tabs and unnecessary spaces, leaving the newline characters.
My C++ code modified to remove the functions ( since you want smaller source code ) comes in around 348 characters and my hybrid C/C++ code comes in at 491 characters.

I used notepad++ to format, removing the spaces and tabs, and get character count.

EDIT: Just wanted to show what I mean;

Code: Select all

#include<cstdio>
#include<cctype>
#define MAX_LENGTH 512
int main(){
char input[MAX_LENGTH]{},output[MAX_LENGTH]{};
size_t out_pos=0;
printf("%s","Enter a string, I'll remove extra spaces. \n");
fgets(input,MAX_LENGTH,stdin);
char c=input[0];
for(size_t i=0;i<MAX_LENGTH&&c!='\0';++i,c=inBuffer[i]){
if(isalpha(c)||ispunct(c)){
output[out_pos]=c;
++out_pos;}
else if(isspace(c)){
if(output[out_pos-1]!=' '){
output[out_pos ]=c;
++out_pos;}}}
printf("%s",output);
return 0;}
This is my hybrid C/C++ code bunched up, looks ugly as hell.
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