Convert java code to c++
Nov 29, 2015 at 9:32pm UTC
i have a code here that needs to be translated into c++ any ideas
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main(int conseclets, char name[]) {
char a[30];
int i;
printf("enter a string\n" );
gets(a);
for (i=0;i<strlen(a);i++)
{
if (a[i]==a[i+1])
{
printf("%c is consecutive\n" ,a[i]);
}
}
return 0;
}
Nov 29, 2015 at 10:43pm UTC
I do not think that is JAVA. Nevertheless, I would convert it to C++ like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include <string>
int main()
{
std::string a;
std::cout << "Enter a string\n" ;
std::cin >> a;
for (int i = 0; i < a.length(); i++)
{
if (a[i] == a[i + 1])
std::cout << a[i] << " is consecutive\n" ;
}
return 0;
}
The least preferred method, in my opinion is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include <cstring>
int main()
{
char a[30];
std::cout << "Enter a string\n" ;
std::cin >> a;
for (int i = 0; i < strlen(a); i++)
{
if (a[i] == a[i + 1])
std::cout << a[i] << " is consecutive\n" ;
}
return 0;
}
Last edited on Nov 29, 2015 at 10:46pm UTC
Nov 29, 2015 at 10:47pm UTC
thanks man it worked great on my compiler its great to know that there's people out there who are willing to help I've been stressed out a lot from c++ programming. much appreciated buddy
Topic archived. No new replies allowed.