| | |
C to C++
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jul 2008
Posts: 46
Reputation:
Solved Threads: 0
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 !
In fact, I just can't figure the "scanf" part out, I think the other part is similar to C++. Thanks in advance ! C++ Syntax (Toggle Plain Text)
#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); }
Last edited by Ancient Dragon; Jul 29th, 2008 at 9:13 am. Reason: add code tags
•
•
Join Date: Jan 2008
Posts: 119
Reputation:
Solved Threads: 10
•
•
•
•
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 CIn 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);
}
]
C++ Syntax (Toggle Plain Text)
#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++.
But to be "just more C++", you may rewrite it as follows:
C++ Syntax (Toggle Plain Text)
#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.
![]() |
Other Threads in the C++ Forum
- Previous Thread: writing to a file
- Next Thread: Files and Arrays
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





