Дана последовательность слов, в которой слова разделены запятыми, а за последним словом — точка. Напечатайте:  а) эту же последовательность слов, но в обратном порядке;  б) все различные слова, указав для каждого из них число его вхождений в последовательность — Pascal(Паскаль)/C++(Си)

 а) эту же последовательность слов, но в обратном порядке;

Var s,st,sv:string;
Begin
    write('Введите строку: ');readln(s);
    if s[Length(s)]='.' then  Delete(s,Length(s),1);
    s:=s+',';sv:='';
    While Pos(',',s)>0 do
    Begin
      st:=Copy(s,1,Pos(',',s)-1);
      Delete(s,1,Pos(',',s));
      sv:=st+','+sv;
    End;
    sv[Length(sv)]:='.';
    writeln('Полученная строка = ',sv);
    readln;
End.

Следующий вариант

uses crt;
var s,s1:string;
    c:char;
    a:array[1..100] of string;
    b:array[1..100] of byte;
    n,i,j:byte;
    f:boolean;
begin
clrscr;
writeln('Введите слова, разделенные запятыми, в конце точка:');
s:='';
repeat
c:=readkey;
write(c);
s:=s+c;
if c='.' then writeln;
until c='.';
s[length(s)]:=',';
n:=1;
a[n]:=copy(s,1,pos(',',s)-1);
b[n]:=1;
delete(s,1,pos(',',s));
while pos(',',s)>0 do
 begin
  s1:=copy(s,1,pos(',',s)-1);
  f:=false;
  j:=1;
  while(j<=n)and not f do
  if a[j]=s1 then
   begin
    f:=true;
    b[j]:=b[j]+1;
   end
  else j:=j+1;
  if not f then
   begin
    n:=n+1;
    a[n]:=s1;
    b[n]:=1;
   end;
  delete(s,1,pos(',',s));
 end;
writeln('Разные слова:');
for i:=1 to n do
writeln(a[i],' ',b[i],' рз.');
readln
end.

C++

#include <iostream>
#include <map>
 
int main()
{
    std::string buf;
    std::map<std::string, int> wordMap;
 
    std::cout << "Enter words: ";
 
    while (std::cin >> buf && std::cin.peek() != '\n')
    {
        wordMap[buf]++;
    }
 
    for (std::map<std::string, int>::iterator i = wordMap.begin(); i != wordMap.end(); i++)
    {
        std::cout << i->first << ": " << i->second << std::endl;
    }
 
    return 0;
}

Следующий вариант

#include <iostream>
#include <cstring>
#include <map>
 
bool Comp(char const* a, char const* b)
{
    return std::strcmp(a, b) < 0;
}
 
int main ()
{
    char s[]  = "table  computer apple  computer table box.", * pDelims = (char*)" .";
    std::map<const char*, std::size_t, bool (*)(char const*, char const*)> map(Comp);
    for(char * p = std::strtok(s, pDelims); p; p = std::strtok(0, pDelims))
    {
        ++map[p];
    }
    for(std::map<char const*, std::size_t, bool(*)(char const*, char const*)>::const_iterator it = map.begin(), itEnd = map.end(); it != itEnd; ++it)
    {
        std::cout << it->first << " - " << it->second << '\n';
    }
    std::cin.sync();
    std::cin.get();
    return 0;
}

Следующий вариант

#include <iostream>
#include <string>
 
int main()
{
    std::string s("  a table  computer apple  computer table a tablespoon.  "), word, tmp;
    std::string::size_type ind(0), ind1(0), ind2, counter(0);
    for(ind = 0; (ind = s.find('.', ind)) != std::string::npos; s.replace(ind, 1, 1, ' '))
    ;
    while((ind = s.find_first_not_of(' ', 0)) != std::string::npos &&
          (ind1 = s.find(' ', ind)) != std::string::npos)
        {
            word.assign(s, ind, ind1 - ind);
            s.erase(ind, ind1 - ind);
            for(counter = 1, ind =  0;
                (ind = s.find_first_not_of(' ', ind)) != std::string::npos &&
                (ind1 = s.find(' ', ind)) != std::string::npos; )
            {
                if(s.substr(ind, ind1 - ind) == word)
                {
                    ++counter;
                    s.erase(ind, ind1 - ind);
                }
                else
                {
                    ind = ind1;
                }
            }
            std::cout << word << " - " << counter << std::endl;
        }
    return 0;
}

б) все различные слова, указав для каждого из них число его вхождений в последовательность.

var
  s, w: string;
  k, p: integer;
begin
  writeln('Введите строку слов, разделённых запятыми, в конце строки точка:');
  readln(s);
  s[length(s)] := ',';
  while length(s) > 0 do
    begin
      w := copy(s, 1, pos(',', s) - 1);
      write(w, ' ');
      k := 0;
      p := 1;
      while (p > 0) and (s[p + length(w)] = ',') and ((p = 1) or (s[p-1] = ',')) do
        begin
          delete(s, p, length(w) + 1);
          inc(k);
          p := pos(w, s)
        end;
      writeln(k)
    end;
  readln
end.

Следующий вариант

var
  s, w: string;
  k, p: integer;
begin
  writeln('Введите строку слов, разделённых запятыми, в конце строки точка:');
  readln(s);
  s[length(s)] := ',';
  s := ',' + s;
  while length(s) > 1 do
    begin
      p := 2;
      while s[p] <> ',' do inc(p);
      w := copy(s, 1, p);
      write(copy(w, 2, length(w) - 2), ' ');
      p := 1;
      k := 0;
      while (p > 0) do
        begin
          delete(s, p, length(w) - 1);
          inc(k);
          p := pos(w, s)
        end;
      writeln(k)
    end;
  readln
end.

Leave a Comment

32 + = 37