C++ :: Aufgabe #22

4 Lösungen Lösungen öffentlich

Text nach Wörtern untersuchen

Anfänger - C++ von Dome - 29.12.2012 um 01:37 Uhr
Ein Text und das Wort was gesucht werden soll, soll eingegeben werden. Daraufhin soll ausgegeben wie oft das Wort in dem Text vorhanden ist.

Konsolenausgabe:

Eingabe Text: Das ist mein Beispieltext.
Welches Wort soll gesucht werden: ist

Das Wort "ist" wurde 1 mal gefunden.

Lösungen:

1x
vote_ok
von Coco (750 Punkte) - 02.01.2013 um 00:11 Uhr
Quellcode ausblenden C-Code
#include <iostream>
#include <string>

using namespace std;

int main()
{
	std::string s_String1, s_String2;
	int Position = 0;

	cout << "Geben sie eine Text ein: ";
	std::getline(std::cin, s_String1);
	

	cout << "Nach welchem Wort soll gesucht werden: ";
	std::getline(std::cin, s_String2);
	
	Position = static_cast<int>(s_String1.find(s_String2, 0));

	if(Position == string::npos)
	{
		cout << "Konnte nicht gefunden werden!" << endl;
	}
	else
	{
		cout << "Ja das Wort " << s_String2 << " wurd in Text gefunden." << endl;
		cout << "An der " << Position + 1 << " Stelle" << endl;
	}


	system("pause");
	return 0;
}


gruß
coco
vote_ok
von Sone (2570 Punkte) - 04.01.2013 um 09:38 Uhr
Schnell hingefrickelt:
Quellcode ausblenden C-Code
#include <string>
#include <iostream>
#include <iterator>
#include <sstream>
#include <algorithm>

template<typename IterT>
size_t count(IterT first1, IterT last1,
             IterT first2, IterT last2)
{
    size_t rval = 0;

    do
        first1 = std::search(first1, last1, first2, last2);
    while( first1 != last1 && (std::advance(first1, std::distance(first2, last2)), ++rval) );

    return rval;
}

template<typename CharT,
          typename CharTraits,
          typename AllocT>
size_t count(std::basic_istream<CharT, CharTraits>& is, std::basic_string<CharT, CharTraits, AllocT> const& str)
{
    size_t rval = 0;
    std::basic_string<CharT, CharTraits, AllocT> tmp;

    do is >> tmp; while( is && ( rval += (tmp == str), true ) );

    return rval;
}

int main()
{
    std::string str;
    std::getline(std::cin, str);
    std::cout << "Zu suchendes Wort: ";
    auto word = *std::istream_iterator<std::string>(std::cin);

    std::istringstream stream(str);
    std::cout << "Die Zeichenfolge \"" << word << "\" kam " << count(str.begin(), str.end(), word.begin(), word.end()) << " mal vor.\n"
                 "Das Wort \"" << word << "\" kam " << count(stream, word) << " mal vor.";
}

Konsolenausgabe:

Dies ist ein Satz in dem Mist steht.
Zu suchendes Wort: ist
Die Zeichenfolge "ist" kam 2 mal vor.
Das Wort "ist" kam 1 mal vor.
vote_ok
von Lemon (470 Punkte) - 18.10.2017 um 07:55 Uhr
Quellcode ausblenden C-Code
#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;


int main()
{

	string eingabeText, eingabeWort, itstring;
	int zaehler = 0;

	cout << "Gib dein Text ein." << endl;
	getline(cin, eingabeText);

	cout << "Gib das zu suchende Wort ein." << endl;
	getline(cin, eingabeWort);


	for(string::iterator it = eingabeText.begin(); it != eingabeText.end(); ++it)
	{
		if (*it == ' ' )
		{
			
			for (int j = 0; j != eingabeWort.size(); ++j)
			{
				++it;
				itstring = itstring + *it ;

				if (itstring == eingabeWort)
				{
					++zaehler;
				}
			}
			itstring = "";
		}

	}

	cout << zaehler;

	system("pause");
    return 0;
}

vote_ok
von NukE (360 Punkte) - 12.05.2019 um 13:14 Uhr
Quellcode ausblenden C-Code
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string wort,text;
    string::size_type index;
    int zaehler=0;


    cout << "Eingabe Text: ";
    getline(cin,text);
    cout << "Welches Wort soll gesucht werden: ";
    getline(cin,wort);
    index=text.find(wort);
    while(index !=string::npos)
    {
        index++;
        zaehler++;
        index=text.find(wort,index);
    }
    cout << "Das Wort \"" << wort << "\" " << "wurde " << zaehler << " mal gefunden.\n";





    return 0;
}
1809131

Du scheinst einen AdBlocker zu nutzen. Ich würde mich freuen, wenn du ihn auf dieser Seite deaktivierst und dich davon überzeugst, dass die Werbung hier nicht störend ist.