Jump to content
Incepat0r

Php to C++

Recommended Posts

Posted

Ma poate ajuta cineva sa transform acest cod php in c++, adica am de returnat de cate ori se gaseste un cuvant citit de la tastatura intr-un fisier

<?
//afiseaza numarul de aparitii al unui cuvant intr-un fisier
$nume_fisier="ex3.txt";
$cuvant='cuvant';
$handle=fopen($nume_fisier,"r") or
die("Eroare la deschidere");
$n=0;
while(!feof($handle))
{
$linie=fgets($handle,4096);
$n += substr_count($linie, $cuvant); ;
}
fclose($handle);
echo "Cuvantul $cuvant apare de $n ori in fisierul $nume_fisier";
?>

Posted (edited)
Ma poate ajuta cineva sa transform acest cod php in c++, adica am de returnat de cate ori se gaseste un cuvant citit de la tastatura intr-un fisier

<?
//afiseaza numarul de aparitii al unui cuvant intr-un fisier
$nume_fisier="ex3.txt";
$cuvant='cuvant';
$handle=fopen($nume_fisier,"r") or
die("Eroare la deschidere");
$n=0;
while(!feof($handle))
{
$linie=fgets($handle,4096);
$n += substr_count($linie, $cuvant); ;
}
fclose($handle);
echo "Cuvantul $cuvant apare de $n ori in fisierul $nume_fisier";
?>

string nume_fisier = "ex3.txt";

string cuvant = "cuvant";

TextReader tr = new StreamReader(nume_fisier );

try {

int n = 0;

string line = null;

while ((line = tr.ReadLine()) != null) {

if (line.Contains(cuvant)) { n++; }

}

}

catch (Exception ex) { MessageBox.Show(ex.toString()); }

tr.Close();

MessageBox.Show("Cuvantul " + cuvant + " apare de " + n + " ori in fisierul + " + nume_fisier + ".";

//netestat. Daca folosesti un 'console application' inlocuieste MessageBox.Show() cu Console.WriteLine();

Edited by fulminator
Posted

Iata si o implementare c++ pura


#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
ifstream fIn;
string cuvant = "asdf"; // cuvantul tau
int counter = 0;
fIn.open("ex3.txt");
if(fIn.is_open())
{
while(fIn.good())
{
string temp;
getline(fIn, temp);
int pos = 0;
pos = temp.find(cuvant, pos);
while(pos != string::npos)
{
counter++;
pos = temp.find(cuvant, (pos + cuvant.size()));
}
}

}
else
{
cout << "Eroare la deschidere fisier.\n";
}
cout << cuvant << " apare de " << counter << " ori.\n";
return 0;
}

Posted
string nume_fisier = "ex3.txt";

string cuvant = "cuvant";

TextReader tr = new StreamReader(nume_fisier );

try {

int n = 0;

string line = null;

while ((line = tr.ReadLine()) != null) {

if (line.Contains(cuvant)) { n++; }

}

}

catch (Exception ex) { MessageBox.Show(ex.toString()); }

tr.Close();

MessageBox.Show("Cuvantul " + cuvant + " apare de " + n + " ori in fisierul + " + nume_fisier + ".";

//netestat. Daca folosesti un 'console application' inlocuieste MessageBox.Show() cu Console.WriteLine();

Firati .net'ul de ras. C# face de rusine limbajul C.

Here you go:

#include <stdio.h>

#include <string.h>

void citire(char *sir,char *subsir)

{

printf("Introdu textul: ");

gets(sir);

printf("Introdu succesiunea de caractere: ");

gets(subsir);

}

int nr_sbsir_sir(char *sir,char *subsir)

{

int nr=0,i,j,x,ok;

for (i=0; i<strlen(sir)-strlen(subsir)+1; i++)

if (sir==subsir[0])

{

ok=1;

x=1;

for (j=1; j<strlen(subsir); j++)

{

if (sir[i+x]!=subsir[j])

ok=0;

x=x+1;

}

if (ok==1)

nr++;

}

return nr;

}

int main()

{

char sir[100],subsir[100];

citire(sir,subsir);

printf("Subsirul \"%s\" apare de %d ori in sirul \"%s\" ",subsir,nr_sbsir_sir(sir,subsir),sir);

return 0;

}

Posted

This document introduces quickly the PHP programming language to those who are already well-versed in the C++ programming language. PHP is a very popular language for publishing web sites with dynamic content. Its strong similarity to C makes the syntax easy to learn for many.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...