Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > C/C++: Для новичков > Удаление последующего вхождения первой буквы слова


Автор: koster26 29.3.2009, 07:19
Прошу помощи в решении следующей задачи:
13. В произвольном тексте, содержащем не более 10 строк, в каждой строке не более 80 символов, удалить из слова все последующие вхождения первой буквы. Полученный текст напечатать. (Суть в том, если слово начинается с буквы "a", то все остальные буквы "a" в этом слове, кроме первой, нужно удалить).
Мой нерабочий текст программы 
Код


#include "stdafx.h"
#include <string.h>
#include <iostream>
#include <stdio.h>



int main()
{
    
    int i=0,j=0;

    char str[] = " SnSoSw crcysctalcs fofrfm whwen titnty supessrcsososled clcoccud drodpledtds frefefze. \nThTeTse drdopdledts aare abale to rermrairn liqluild at temtpertattures colcder than 0°C \nbecbabuse in orodoer to freeze, a few molecules in the liquid droplet need \nto get together by chance to form an arrangement close to that in an ice lattice \nthen the droplet freezes. In warmer clouds an aerosol particle or ice \nnucleus must be present in (or in contact with) the droplet to act as a nucleus.";


    int size_of_text = strlen(str)+1;

    printf("Size of text: %d\n\n",size_of_text);


    char temp={0};


    for(i=0;i<size_of_text;i++)
    {    
    

            if(str[i]==' ')
            {
                str[i+1]=temp;
                j=i+2;
                while (str[j]!=' ')
                {
                    if(str[j]==temp)
                    {
                        str[j]='!';
                    }
                }
            }

        

//вывод готового текста:
    for(i=0;i<size_of_text;i++)
    {    
        if(str[i]!='!')
        printf("%c",str[i]);

        if(str[i]=='\0')
            break;
    }


getchar();
getchar();
return 0;
}
 

Автор: zim22 29.3.2009, 08:30
Код
#include <cctype>
#include <string>

int main()
{
  std::string text("    Hello  mmy dddddear frienddd. I love you      ");  
  
  std::string::size_type pos = 0;
  while ((pos = text.find_first_of(" ", pos)) != std::string::npos) {
    ++pos;
    if (text.size() - 1 >= pos) {    
      while (text[pos] == text[pos + 1] && isalpha(text[pos])) {
        text.erase(pos, 1);    
      }
    }
  }
  return 0;
}


Автор: afanp 29.3.2009, 12:12
Код

#include <string>
#include <iostream>
#include <algorithm>
using namespace std;


int main(){
    string s1,s2;
    getline(cin,s1);
    s1+=" ";
    for(int i=0;i<s1.length();i++){
        s2+=s1[i];
        if(s1[i]==' '){
            string a;
            a=s2[0];
            s2.erase(remove(s2.begin()+1,s2.end(),a[0]),s2.end());
            cout << s2 << " ";
            s2.clear();}
    }
    system("pause");
    return 0;
}

Автор: koster26 30.3.2009, 11:36
zim22, 
afanp, Огромное спасибо за оперативнаю помощь!

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)