Форум С++

 

Ответить на сообщение

Вернуться к теме

Вы отвечаете на сообщение:

Автор: StarWorm   (17.11.2008 в 02:23)
Написал за вечер... Немного отладил! Посмотри, вроде бы работает...
Расставляет нужное количество пробелов между словами, чтобы получалась требуемая длина строки!
P.S. написана на Visual Studio 2008 C++


#include "stdafx.h"
#include "conio.h"
#include <iostream>
//#include <string>



using namespace std;

int length(char* str)
{
int i=0;
while(str[i++]);
return --i;
}

charfillStr(char* str, int count)
{
bool flag=false;
char* ptrStr=str;
int ind=0;
while(count>0)
{
if(ptrStr[ind]==NULL)
{
if(flag==false)
{
char* tmpStr=ptrStr;
ptrStr=new char[length(ptrStr)+1];
int j=0;
while(tmpStr[j]!=NULL)
ptrStr[j++]=tmpStr[j];
ptrStr[j]=NULL;
return ptrStr;
}
else
{
ind=0;
flag=false;
}
}
else if(ptrStr[ind]==' ')
{
char* tmpStr=ptrStr;
ptrStr=new char[length(ptrStr)+2];
int j=0,k=0;
while(tmpStr[j]!=NULL)
{
ptrStr[k]=tmpStr[j];
if(j==ind)
{
ptrStr[k]=tmpStr[j];
ptrStr[++k]=tmpStr[j];
}
j++;
k++;
}
ptrStr[k]=NULL;
count--;
while(ptrStr[++ind]==' ');
flag=true;
}
else ind++;
}
return ptrStr;
}


charresizeStr(char* str,int size)
{
int count=size-length(str);
char* ptrStr=str;
ptrStr=fillStr(ptrStr,count);
return ptrStr;
}

void main ()
{
int maxLength;
char* myStr = new char[255];
cout<<"Enter your sentence: ";
cin.getline(myStr,255);
cout<<"Enter max length of string: ";
cin>>maxLength;
char* resultStr=new char[maxLength];
resultStr=resizeStr(myStr,maxLength);
cout<<myStr<<endl;
cout<<resultStr<<endl;

getch();
delete [] resultStr;
delete [] myStr;
}


Ваше имя:

Пароль:

Цитировать

Используйте тэги для выделения текста:
Код: [code][/code]
Жирный: [b][/b]
Наклонный: [i][/i]
URL: [url][/url]

Сообщение:

Прикрепить: