Замена пробелов на троеточия....
Помогите написать код, вот моя как бы программа
Код:
#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
int amountSpace (const char*);
int main()
{
char tmp [80];
int p;
char* ptr;
do
{
cout <<"Enter tmp-> ";
cin.getline (tmp,80);
ptr=new char [strlen (tmp)+1];
strcpy_s (ptr, strlen (tmp)+1, tmp);
cout <<"\nBefore\t" << ptr << endl; //строка до обработки
p=amountSpace(ptr);
}
for(; *s; *s++) { // замена 3-пробела троеточиями
for(p = ptr; (*p) && (*p == ' '); *p++);
if((p - ptr) >= 3) {
p -= (p - ptr) - 3;
while(ptr < p)
*ptr++ = '.';
*ptr--;
}
cout<<"Probel -> "<<p<<endl;
cout <<"After\t" << ptr << endl<<endl; //строка после обработки
delete [] ptr;
}
while (1);
return 0;
}
int amountSpace (const char* pS) // Вычеслить в строке количество пробелов
{
int n(0); const char* p(pS);
while (*p)
{
if (isspace (*p)) ++n;
p++;
}
return n;
}
#include <cctype>
#include <cstring>
using namespace std;
int amountSpace (const char*);
int main()
{
char tmp [80];
int p;
char* ptr;
do
{
cout <<"Enter tmp-> ";
cin.getline (tmp,80);
ptr=new char [strlen (tmp)+1];
strcpy_s (ptr, strlen (tmp)+1, tmp);
cout <<"\nBefore\t" << ptr << endl; //строка до обработки
p=amountSpace(ptr);
}
for(; *s; *s++) { // замена 3-пробела троеточиями
for(p = ptr; (*p) && (*p == ' '); *p++);
if((p - ptr) >= 3) {
p -= (p - ptr) - 3;
while(ptr < p)
*ptr++ = '.';
*ptr--;
}
cout<<"Probel -> "<<p<<endl;
cout <<"After\t" << ptr << endl<<endl; //строка после обработки
delete [] ptr;
}
while (1);
return 0;
}
int amountSpace (const char* pS) // Вычеслить в строке количество пробелов
{
int n(0); const char* p(pS);
while (*p)
{
if (isspace (*p)) ++n;
p++;
}
return n;
}
Код:
int main()
{
char Line[] = "Hello, World! It is the test of my string \n";
cout<<"Your Line: "<<Line<<endl;
strcpy_s(Line, strlen(Line)+1, SpaceToPoint(Line));
cout<<"Modification Line: "<<Line<<endl;
system("pause");
return 0;
}
char* SpaceToPoint(char* Line)
{
unsigned int SpaceIndexBegin = 0, SpaceCount = 0;
for(unsigned int idx=0;idx<strlen(Line);idx++)
{
if(Line[idx] == ' ')
{
if(SpaceCount == 0)
{
SpaceIndexBegin = idx;
SpaceCount ++;
}
else SpaceCount++;
}
else
{
if(SpaceCount >= 3)
{
Line = SwapSpaceToPoint(Line, SpaceIndexBegin, SpaceCount);
SpaceIndexBegin = 0;
SpaceCount = 0;
}
else
{
SpaceIndexBegin = 0;
SpaceCount = 0;
}
}
}
return Line;
}
char* SwapSpaceToPoint(char* Line, unsigned int index, unsigned int count)
{
for(unsigned int idx=index; idx<(index+count);idx++)
{
Line[idx] = '.';
}
return Line;
}
{
char Line[] = "Hello, World! It is the test of my string \n";
cout<<"Your Line: "<<Line<<endl;
strcpy_s(Line, strlen(Line)+1, SpaceToPoint(Line));
cout<<"Modification Line: "<<Line<<endl;
system("pause");
return 0;
}
char* SpaceToPoint(char* Line)
{
unsigned int SpaceIndexBegin = 0, SpaceCount = 0;
for(unsigned int idx=0;idx<strlen(Line);idx++)
{
if(Line[idx] == ' ')
{
if(SpaceCount == 0)
{
SpaceIndexBegin = idx;
SpaceCount ++;
}
else SpaceCount++;
}
else
{
if(SpaceCount >= 3)
{
Line = SwapSpaceToPoint(Line, SpaceIndexBegin, SpaceCount);
SpaceIndexBegin = 0;
SpaceCount = 0;
}
else
{
SpaceIndexBegin = 0;
SpaceCount = 0;
}
}
}
return Line;
}
char* SwapSpaceToPoint(char* Line, unsigned int index, unsigned int count)
{
for(unsigned int idx=index; idx<(index+count);idx++)
{
Line[idx] = '.';
}
return Line;
}