Hi, I would like to ask you for help in converting this code from C to C++. It's my little friend 's homework, but I can't help him 'cause I know nothing about C :( In fact, I just can't figure the "scanf" part out, I think the other part is similar to C++. Thanks in advance !

#include <stdio.h>
#include <string.h>
void main()
{
int i,br=0,d=0;
char r[200];
printf("Vnesete Recenica\n");
gets(r);
d=strlen(r);
for(i=0;i<=d;i++)
{
if((r[i]=='n')&&(r[i+1]=='o'))
{
r[i]=' ';
r[i+1]=' ';
br++;
}
}
printf("Vo Vasata recenica se pronajdeni i izbrisani %d no zborovi\n",br);
scanf("%d",&i);
}

Recommended Answers

All 3 Replies

First of all use code tags. Second do not use void main..main has to always return something. Then, what is the program supposed to do and what dont you understand about scanf..what kind of errors are you getting when you compile your code?

Hi, I would like to ask you for help in converting this code from C to C++. It's my little friend 's homework, but I can't help him 'cause I know nothing about C :( In fact, I just can't figure the "scanf" part out, I think the other part is similar to C++. Thanks in advance !
[
#include <stdio.h>
#include <string.h>
void main()
{
int i,br=0,d=0;
char r[200];
printf("Vnesete Recenica\n");
gets(r);
d=strlen(r);
for(i=0;i<=d;i++)
{
if((r=='n')&&(r[i+1]=='o'))
{
r=' ';
r[i+1]=' ';
br++;
}
}
printf("Vo Vasata recenica se pronajdeni i izbrisani %d no zborovi\n",br);
scanf("%d",&i);
}

]

#include <iostream>
#include <string>

void main()
{
	int i,br=0,d=0;
	//char r[200];
	std::string r;
	//printf("Vnesete Recenica\n");
	std::cout<<"Vnesete Recenica\n"<<std::endl;
	//gets(r);
	std::cin>>r;
	//d=strlen(r);
	//for(i=0;i<=r.length();i++)
	/*
	for(i=0;i<r.length() - 1 ;i++) //THIS WAS INCORRECT IN THE 1st IMPLEMENTATION  TOO, BUFFER OVERRUN !!
	{
		if((r.at(i)=='n')&&(r.at(i+1)=='o'))
		{
			r[i]=' ';
			r[i+1]=' ';
			br++;
		}
	}
	*/
	std::string::size_type poz = -1;
	while ( true )
		if (  ( poz = r.find( "no", poz + 1 ) ) != std::string::npos )
			br++;
		else 
			break;
	

	//printf("Vo Vasata recenica se pronajdeni i izbrisani %d no zborovi\n",br);
	std::cout<<"Vo Vasata recenica se pronajdeni i izbrisani "<<br<<" no zborovi\n"<<std::endl;
	//scanf("%d",&i);
	std::cin>>i;
}

the last scanf/cin is placed there so that the program won't end untill the user sees the output and presses some key.
I placed the old C implementation between coments. It was also incorrect as it was causing buffer overuns on the char*

Hi, I would like to ask you for help in converting this code from C to C++.

Code, that you posted is correct C++ code, except nonstandard main declaration.

But to be "just more C++", you may rewrite it as follows:

#include <string>
#include <iostream>

int main(void)
{  
  std::string r;
  std::cout << "Vnesete Recenica" << std::endl;
  std::getline(std::cin, r);
  size_t d = r.size();
  int br = 0;
  for(int i = 0; i != d; ++i){
    if((r[i]=='n')&&(r[i+1]=='o')){
      r[i]=' ';
      r[i+1]=' ';
      br++;
    }
  }
  std::cout << "Vo Vasata recenica se pronajdeni i izbrisani " << br << " no zborovi" << std::endl;
  std::cin >> d;
  return 0;
}

Also, we may use iterators and std::replace from <algorithm> header to make this code "really C++", but this simple task you may solve by yourself, I think.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.