Skip to content

Instantly share code, notes, and snippets.

@bentrevett
Created March 12, 2017 16:41
Show Gist options
  • Save bentrevett/8cf8a52dfea0cc9b51400f14706d3a56 to your computer and use it in GitHub Desktop.
Save bentrevett/8cf8a52dfea0cc9b51400f14706d3a56 to your computer and use it in GitHub Desktop.
#ifdef _WIN32
#include <Windows.h>
#define muhSleep(x) Sleep((x))
#else
#include <unistd.h>
#define muhSleep(x) sleep((x))
#endif
void fillOffsets( int **offsets, char *str )
{
int n = strlen(str), base = 50, spaces = 0,
b = rand() % 4 + 3, stutters = rand() % 4 + 1;
*offsets = calloc(sizeof(int), n);
for (int i = 0; i < n; i++)
{
switch (str[i])
{
case ' ':
spaces++;
if (spaces == b)
{
(*offsets)[i] = base * 5 + 10 * (5 + rand() % 5);
spaces = 0;
b = rand() % 3 + 2;
}
else
(*offsets)[i] = base;
break;
default:
(*offsets)[i] = base + 10 + rand() % 10;
break;
}
}
for (int j = 0; j < stutters; j++)
(*offsets)[rand() % n] = -1;
}
int main( void )
{
int i, *offsets = 0;
char Buf[1000] = "How can I print text in c which make it feels like someone is typing?\nLike characters would print one after another pausing for a brief 1 to 2 secs?";
int n = strlen(Buf);
getchar();
srand((int)time(NULL));
fillOffsets(&offsets, Buf);
for (int i = 0; i < n; i++)
{
printf("%c", Buf[i]);
if (offsets[i] == -1)
{
offsets[i] = 50;
for (int k = 0; k < 4; k++, i--)
{
printf("\b \b");
muhSleep(50);
}
if (i < 0)
i = 0;
muhSleep(500);
}
else
muhSleep(offsets[i]);
}
getchar();
free(offsets);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment