Im trying to do the C version of something like that in c++:
Code:
#include <string>
#include <bits/stdc++.h>
#include <cstdio>
std::string reverseString (std::string str )
{
std::string result = "";
for (int i = str.length()-1 ; i > -1 ; i--)
{
result += str[i];
}
return result;
}
int main()
{
std::vector<std::string> Tvec(0xdeaad,"Test_Strings");
std::cout << "Running for " << Tvec.size() << " test cases." << "\n";
auto t1 = std::chrono::high_resolution_clock::now();
for (auto &s : Tvec)
{
std::string tested = reverseString(s);
}
auto t2 = std::chrono::high_resolution_clock::now();
auto duration1 = std::chrono::duration_cast<std::chrono::microseconds>(t2-t1).count();
std::cout << "Duration1 : " << duration1 << "\n";
return 0;
}
I have wrote this so far but it seems all bad and wrong on so many different levels:
Code:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
char reverseString4(char *str)
{
size_t strsize = strlen(str);
char result[strsize];
for (size_t i = strsize; i > 0; --i) {
result = str;
}
return result;
}
int main(void)
{
int test_runs = 4096;
const char *test_word = "deadbeef";
size_t test_word_len = strlen(test_word);
char *TEST_ARR[test_runs][test_word_len];
for (size_t i = 0; i < test_runs; i++) { //Need to find a way to benchmark the exec time.
TEST_ARR = test_word;
}
return 0;
}
Is there any way I can achieve something like from that first cpp code but in c?