Thank you! It helped, and I was able to pull it off. However, I have a new issue. I'm trying to get a character count, and I'm having an issue with any showing up. Perhaps you could help me with what I'm messing up on:
Code:
#include <stdio.h>#include <stdlib.h>
#include <string.h>
int main()
{
char str1[255], str2[255], combinedStr [255], newSentence [101];
int lenOfStr1, lenOfStr2, lenOfNewSent, newSentenceArray [128]={0}, totalArray [128]={0};
char currentChar, currentChar2;
int i = 0, totLtrs = 0;
double percentage = 0.00;
printf("Please enter a string of 254 characters or less:");
fgets(str1,254,stdin);
printf("Please enter a second string of 254 characters or less:");
fgets(str2,254, stdin);
lenOfStr1 = strlen (str1);
lenOfStr2 = strlen (str2);
str1[lenOfStr1 - 1] = '\0';
str2[lenOfStr2 - 1] = '\0';
printf("Your first string is %d characters long.\n", lenOfStr1);
printf("Your second string is %d characters long.\n", lenOfStr2);
strncpy(combinedStr, str1, lenOfStr1 / 2);
combinedStr[lenOfStr1 / 2] = '\0';
strcat(combinedStr, str2 + lenOfStr2 / 2);
printf("String 3 is %s\n", combinedStr);
printf("Now, please enter a sentence of up to 100 characters:");
fgets(newSentence, 100, stdin);
lenOfNewSent = strlen (newSentence);
newSentence[lenOfNewSent - 1] = '\0';
while (newSentence[i] != '\0')
{
currentChar = newSentence[i];
newSentenceArray[currentChar]++;
++i;
}
totLtrs = i;
printf("\nFor the string you just wrote\n\n\n", i);
printf("FREQUENCY TABLE\n-------------------\nChar Count %% of Total\n");
printf("---- ----- ----------\nALL\t%d\t100.00%%\n", totLtrs, percentage);
for (i=0; i < 128; ++i)
{
if (newSentenceArray[i] > 0)
{
printf("'%c'\t%d\t%-2.2f%%\n", i, newSentenceArray[i], percentage = ((double) newSentenceArray[i]/(double) totLtrs) * 100.00);
}
}
strcpy (combinedStr, str1);
strcat (combinedStr, str2);
strcat (combinedStr, newSentence);
lenOfNewSent = strlen (combinedStr);
combinedStr[lenOfNewSent - 1] = '\0';
i=0;
printf("The new combinesStr is %s", combinedStr);
while (combinedStr[i] != '\0')
{
currentChar2 = totalArray[i];
totalArray[currentChar2]++;
++i;
}
totLtrs = i;
printf("i is %d", i);
printf("\nFor everything you've written\n\n\n");
printf("FREQUENCY TABLE\n-------------------\nChar Count %% of Total\n");
printf("---- ----- ----------\nALL\t%d\t100.00%%\n", totLtrs);
for (i=0; i < 128; ++i)
{
if (totalArray[i] > 0)
{
printf("'%c'\t%d\t%-2.2f%%\n", i, totalArray[i], percentage = ((double) totalArray[i]/(double) totLtrs) * 100.00);
}
}
return 0;
}
The first loop works perfectly, but I can't get anything with the second. Any ideas?