0
No.5 Lab 6_строки
Posted by ေမၿမဳိ့သား
on
7:18 AM
in
C++ No.5 for 2nd year
Qus==>> Дана
строка слов, разделенных пробелами. Сформируйте строку из неповторяющихся слов,
расположив их в алфавитном порядке.
#include "stdafx.h"
#include "iostream"
#include "string"
#include "stdio.h"
#include "conio.h"
#include <sstream>
using namespace std;
void main ()
{
char str[100];
cout<<"Enter ur string: ";
cin.getline(str,100);
cout<<endl;
string ostr;
string temp[20];
string result[20];
string sortresult[20];
int index[20];
int i,j;
int count=0,countt=0;
istringstream iss(str);
while(getline(iss, ostr, ' '))
{
index[count]=0;
temp[count]=ostr;
cout<<temp[count]<<" ";//dar ka pyan yay thar !!
count++;
}
cout<<endl;
for(i=0;i<count;i++)
{
bool add=true;
for(j=i+1;j<count;j++)
if(temp[i].compare(temp[j])==0)
{
//finding index for repeated words
index[j]=1;
}
}
string strchange;
for(i=0;i<count;i++)
if(index[i]!=1)
{
result[i]=temp[i];
cout<<result[i]<<" ";
}
else
countt++; //to count how many times of same words
cout<<endl;
for(i=0;i<count;i++)
{
for(j=i+1;j<count;j++)
{
if(result[i].compare(result[j])>0)
{
strchange=result[j];
result[j]=result[i];
result[i]=strchange;
}
}
}
cout<<"\n Final Sorting results: "<<endl;// dar ka sorting si thar!!
for(i=0;i<count;i++)
if(result[i]!="")
cout<<result[i]<<" ";
getch();
}
Post a Comment