Skip to content

Instantly share code, notes, and snippets.

@avoidedlife
Forked from anonymous/gist:4773146
Created February 18, 2013 07:38
Show Gist options
  • Save avoidedlife/4975691 to your computer and use it in GitHub Desktop.
Save avoidedlife/4975691 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <strings.h>
#include <math.h>
#define SIZE 255
int main(void)
{
char
string_1[SIZE],
string_2[SIZE],
string_3[SIZE],
string_4[SIZE],
string1_n_string2_merge[SIZE]= {'\0'};
// Asking for user enter string 1
while (1)
{
printf("Enter a string less than 254 chars: ");
if (fgets(string_1, sizeof(string_1), stdin) == 0)
break; // EOF or error
size_t len_of_string1 = strlen(string_1);
if (len_of_string1 > 0)
string_1[--len_of_string1] = '\0'; // Zap newline
if (len_of_string1 <= 254)
break;
}
// Asking for user enter string 2
while (1)
{
printf("Enter a string less than 254 chars: ");
if (fgets(string_2, sizeof(string_2), stdin) == 0)
break; // EOF or error
size_t len_of_string2 = strlen(string_2);
if (len_of_string2 > 0)
string_2[--len_of_string2] = '\0'; // Zap newline
if (len_of_string2 <= 254)
break;
}
// Prints byte lenght of string 1 and string 2
printf("The string 1 is %zu bytes long and string 2 is %zu is bytes long \n",
strlen(string_1), strlen(string_2));
// Partial lenghts of strings for string manipulation //
char
str1_half = strlen(string_1) / 2,
str2_half = strlen(string_2) / 2,
lasth_str2 = strlen(string_2) - str2_half;
// String merge
strncpy(string1_n_string2_merge, string_1,
str1_half);
strncpy(&string1_n_string2_merge[str1_half],
&string_2[str2_half], lasth_str2);
printf("%s\n", string1_n_string2_merge);
// Asking for user enter string 3
while (1)
{
printf("Enter a string less than 254 chars: ");
if (fgets(string_3, sizeof(string_3), stdin) == 0)
break; // EOF or error
size_t len_of_string1 = strlen(string_3);
if (len_of_string1 > 0)
string_3[--len_of_string1] = '\0'; // Zap newline
if (len_of_string1 <= 254)
break;
}
// Full lenghts of strings 1,2,3 for counting input by user //
strcat(string_4, string_1);
strcat(string_4, string_2);
strcat(string_4, string_3);
// Full lenghts of strings 1 & 2 & 3for counting input by user //
unsigned long
lh_full_1 = strlen(string_1),
lh_full_2 = strlen(string_2),
lh_full_3 = strlen(string_3);
float
full_count = lh_full_1 + lh_full_2 + lh_full_3;
printf("\n---FREQUENCY TABLE---\n");
printf("\n----------------------\n");
printf("\nChar Count %% of Total\n");
printf("All \t%.2f %% 100 \n", full_count); // Test output
int lowCount = 0, lowerArray[26] = {0};
while ( string_4[lowCount] != '\0' )
{
/* Considering characters from 'a' to 'z' only */
if ( string_4[lowCount] >= 'a' && string_4[lowCount] <= 'z' )
lowerArray[string_4[lowCount]-'a']++;
lowCount++;
}
for ( lowCount = 0 ; lowCount < 26 ; lowCount++ )
{
if( lowerArray[lowCount] != 0 )
printf(" \"%c\" \t%d \t %% \t %0.2f \n",lowCount+'a',
lowerArray[lowCount], ((lowerArray[lowCount]/full_count)*100));
}
int upperCount = 0, upperArray[26] = {0};
while ( string_4[upperCount] != '\0' )
{
/* Considering characters from 'A' to 'Z' only */
if ( string_4[upperCount] >= 'A' && string_4[upperCount] <= 'Z' )
upperArray[string_4[upperCount]-'A']++;
upperCount++;
}
for ( upperCount = 0 ; upperCount < 26 ; upperCount++ )
{
if( upperArray[upperCount] != 0 )
printf(" \"%c\" \t%d \t %% \t %0.2f \n",upperCount+'A',
upperArray[upperCount],
((upperArray[upperCount]/full_count)*100));
}
int spaceCount = 0, spaceArray[120] = {0};
while ( string_4[spaceCount] != '\0' )
{
/* Considering characters from ' ' to ' ' only */
if ( string_4[spaceCount] >= ' ' && string_4[spaceCount] <= ' ' )
spaceArray[string_4[spaceCount]-' ']++;
spaceCount++;
}
for ( spaceCount = 0 ; spaceCount < 120 ; spaceCount++ )
{
if( spaceArray[spaceCount] != 0 )
printf(" \"%c\" \t%d \t %% \t %0.2f \n",spaceCount+' ',
spaceArray[spaceCount],
((spaceArray[spaceCount]/full_count)*100));
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment