So I have an assignment to format text according to rules and eventually print out the number of characters in the string (including \n
and spaces, anything but the \0
at the end of the string).
Basically, a valid input is ended with a dot but I think I have a few more whitespaces after the final dots.
I have tried several approaches such as loop that replaces spaces with \0
starting from the end of the string.
nothing seems to work though...
EDIT The requirements are:
- to convert double-dots(..) to a new line
- delete multiple spaces leaving only one,
- making sure that there isn't a space prior to a comma or a dot
- making sure that there is single space after a comma or dot.
- not changing the original content between apostrophes.
- and validating that there are Capital letters only in the correct places (new line\paragraph).
we are required to do all of the code in the main function (unfortunately) and me code usually mistake the characters count by 1-2 extra in the count (probably do to extra spaces after the last dot
this is an example of input that my code fails at counting characters
the LANGUAGE "C" is a procedural programming language .It was initially developed by "Dennis Ritchie".. the Main feAtures of "C" language include low-level access to memory, simple set of keywords, and clean style .
int main() {
char ans;
printf("*************** Welcome to the text cleaner ***************\n\n");
do
{
int length, i, j = 0;
int word, sentence, para, space;
char tin[601], tout[601], * dex, * pos;
printf("\nPlease enter text:\n");
gets_s(tin, 600);
length = strlen(tin);
dex = strchr(tin, '.'); //converts double dots to new line
while (dex != NULL)
{
if (tin[dex - tin + 1] == '.') {
tin[dex - tin + 1] = '\n';
}
dex = strchr(dex + 1, '.');
}
length = strlen(tin);
dex = strchr(tin, ' '); //converting multiple spaces to single space
while (dex != NULL)
{
while (dex != NULL && tin[dex - tin + 1] == ' ')
{
for (i = dex - tin + 1; i < strlen(tin); i++)
{
tin[i - 1] = tin[i];
}
dex = strchr(dex, ' ');
j++;
}
dex = strchr(dex + 1, ' ');
}
tin[length - j] = '\0';
j = 0;
dex = strchr(tin, '\n');
while (dex != NULL && tin[dex-tin+1] == ' ') //delets spaces in the beggining of new row
{
for (i = dex - tin + 1;i < strlen(tin);i++) {
tin[i] = tin[i + 1];
}
dex = strchr(dex + 1, '\n');
}
dex = strchr(tin, ','); //deletes space before comma
while (dex != NULL && tin[dex - tin - 1] == ' ')
{
for (i = dex - tin - 1; i < strlen(tin); i++)
{
tin[i] = tin[i+1];
}
dex = strchr(dex+1, ',');
}
dex = strchr(tin, '.'); //deletes space before dots
while (dex != NULL && tin[dex - tin - 1] == ' ')
{
for (i = dex - tin - 1; i < strlen(tin); i++)
{
tin[i] = tin[i+1];
}
dex = strchr(dex + 1, '.');
}
dex = strchr(tin, ','); // adds space after comma
while (dex != NULL && tin[dex - tin + 1] != ' ')
{
if (tin[dex - tin + 1] != '\n')
{
tin[strlen(tin) + 1] = '\0';
for (i = strlen(tin); i > dex - tin; i--)
{
if (i == dex - tin + 1)
{
tin[i] = ' ';
}
else
{
tin[i] = tin[i - 1];
}
}
dex = strchr(dex + 1, ',');
}
}
dex = strchr(tin, '.'); // adds space after dot
while (dex != NULL && tin[dex - tin + 1] != ' ')
{
tin[strlen(tin) + 1] = '\0';
if (tin[dex - tin + 1] == '\n')
{
dex = strchr(dex + 1, '.');
}
else
{
for (i = strlen(tin); i > dex - tin; i--)
{
if (i == dex - tin + 1)
{
tin[i] = ' ';
}
else
{
tin[i] = tin[i - 1];
}
}
dex = strchr(dex + 1, '.');
}
}
strcpy_s(tout, sizeof(tout), tin);
_strlwr_s(tout,sizeof(tout)); //copies and lowercasing the input string
dex = strchr(tin, '"');
if (dex != NULL) {
pos = strchr(dex + 1, '"');
while (dex != NULL)
{
for (i = dex - tin; i < pos - tin; i++) {
tout[i] = tin[i];
}
dex = strchr(pos + 1, '"');
if (dex)
{
pos = strchr(dex + 1, '"');
}
} //making sure that the letters in the quotes have't been lowercased
}
_strupr_s(tin, sizeof(tin));
dex = strchr(tout, '.');
pos = strchr(tin, '.');
while (dex != NULL && pos != NULL)
{
tout[dex - tout + 2] = tin[pos - tin + 2];
dex = strchr(dex + 1, '.');
pos = strchr(pos + 1, '.');
}
//CAPSLOCK
dex = strchr(tout, '.'); //deletes space before dots
while (dex != NULL)
{
if (tout[dex - tout - 1] == ' ')
{
for (i = dex - tout - 1; i < strlen(tout); i++)
{
tout[i] = tout[i+1];
}
}
dex = strchr(dex + 1, '.');
}
if (tout[0] == ' ') {
for (i = 0 ;i < strlen(tout); i++) {
tout[i] = tout[i + 1];
}
}//handeling single space in the beggining of the string
if (tout[0] >= 'a' && tout[0] <= 'z') {
tout[0] -= 32;
} //First letter always capital
word = 0;
sentence = 0;
para = 1;
space = 0;
length = strlen(tout);
for (i = 0; tout[i] != '\0';i++)
{
if (tout[i] == ' ' && tout[i + 1] != ' ')
word++;
}
dex = strchr(tout, '.');
while (dex != NULL)
{
sentence++;
dex = strchr(dex + 1, '.');
}
dex = strchr(tout, '\n');
while (dex != NULL)
{
space++;
para++;
word++;
dex = strchr(dex + 1, '\n');
}
//dex = strchr(tout, '-');
//while (dex != NULL)
//{
// word++;
// dex = strchr(dex + 1, '-');
//}
printf_s("\nText after cleaning:\n------------------------------------------------------------------------------------------------\n");
printf_s("%s\n\n", tout);
printf_s("characters: %d | words: %d | sentences: %d | paragraphs: %d\n------------------------------------------------------------------------------------------------\n",length, word, sentence, para);
printf_s("\nIf you want to clean another string press (y): ");
scanf_s(" %c", &ans, 1);
if (ans == 'y')
{
gets_s(tin, 600);
}
} while (ans =='y');