Доброго времени суток! Может кто сможет помочь, в общем нужно написать функцию которая возвращает строку с тремя пробелами между слов, начало и конец строки не должны быть с пробелом
пишу на c вот что получается
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Function to replace a string with another
// string
char* replaceWord(const char* s, const char* oldW,
const char* newW)
{
char* result;
int i, cnt = 0;
int newWlen = strlen(newW);
int oldWlen = strlen(oldW);
// Counting the number of times old word
// occur in the string
for (i = 0; s[i] != '\0'; i++) {
if (strstr(&s[i], oldW) == &s[i]) {
cnt++;
// Jumping to index after the old word.
i += oldWlen - 1;
}
}
// Making new string of enough length
result = (char*)malloc(i + cnt * (newWlen - oldWlen) + 1);
i = 0;
while (*s) {
// compare the substring with the result
if (strstr(s, oldW) == s) {
strcpy(&result[i], newW);
i += newWlen;
s += oldWlen;
}
else
result[i++] = *s++;
}
result[i] = '\0';
return result;
}
char* expand_string(char* param_1)
{
int count = 0;
char* result=malloc(strlen(param_1)+1);
for(int i=0; param_1[i]!='\0'; i++)
{
if (param_1[i]!=' ')
{
result[count]=param_1[i];
count++;
}
else
{
if(i==0)
{
continue;
}
else if (param_1[i+1]==' '&¶m_1[i+2]==' '&¶m_1[i+3]==' ')
{
continue;
}
else if (param_1[i+1]==' '&¶m_1[i+2]!=' ')
{
char* c=" ";
char* d=" ";
result = replaceWord(param_1, c, d);
count+=3;
}
else
{
result[count]=param_1[i];
count++;
}
}
}
result[count]='\0';
return result;
}
int main()
{
char* result=expand_string(" see what will happen next");
printf("%s\n", result);
free(result);
}
вывод таков: