add utility function replace_multiple_space_with_one()

use it to parse the interactive input
This commit is contained in:
2024-06-27 19:18:28 +01:00
parent 2dda32ead9
commit 1d71f38d39
3 changed files with 54 additions and 4 deletions

38
src/util.c Normal file
View File

@@ -0,0 +1,38 @@
#include <string.h>
void replace_multiple_space_with_one(char *s)
{
int j = 0;
int count = 0;
int len = strlen(s);
if (len == 1 && (s[0] == ' ' || s[0] == '\t'))
{
s[0] = '\0';
return;
}
if (len < 2)
return;
for (int i = 0; s[i] != '\0'; i++)
{
if (s[i] == ' ' || s[i] == '\t')
{
count++;
}
if (s[i] != ' ' && s[i] != '\t')
{
if (count >= 1)
{
count = 0;
s[j++] = ' ';
}
s[j++] = s[i];
}
}
s[j] = '\0';
return;
}