int reverse(int num) { char temp[6]; int ret; sprintf(temp, "%d", num); strrev2(temp); sscanf(temp, "%d", &ret); return ret; }
int main() { char s[] = "blah"; char* output = strrev2(s); printf("%s\n", output); ///char t[] = "abcde"; //output = strrev2(t); //printf("%s\n", output); return 0; }
// Basic: return type int, but not returning anything int main() { // Sol 1 - c style string const char* str = "Madam, I'm Adam"; char buffer[32]; std::strncpy(buffer, str, sizeof(buffer)); char* new_str = strrev1(buffer); // Medium: std::cout vs "\n" vs '\n' std::cout << new_str << '\n'; // Sol2 std::string str2 = "Madam, I'm Adam"; // Medium: Lifetime extension of primary const auto& new_str2 = strrev2(str2); std::cout << new_str2 << '\n'; }
int main() { char s[] = "test string"; char *p = NULL; p = (char *) malloc(sizeof(char) * strlen("some string") + 1); strcpy(p, "some string"); printf("strrev1(%s) = ", s); printf("%s \n", strrev1(s)); printf("strrev1(%s) = ", p); printf("%s \n", strrev1(p)); free(p); // ---------------------------------- // char s1[] = "random dandom"; p = NULL; p = (char *) malloc(sizeof(char) * strlen("what is this?") + 1); strcpy(p, "what is this?"); printf("strrev2(%s) = ", s1); printf("%s \n", strrev2(s1)); printf("strrev2(%s) = ", p); printf("%s \n", strrev2(p)); free(p); // ----------------------------------- // char s2[] = "okie dokie"; p = NULL; p = (char *) malloc(sizeof(char) * strlen("i don't know!!") + 1); strcpy(p, "i don't know!!"); printf("strrev3(%s) = ", s2); printf("%s \n", strrev3(s2)); printf("strrev3(%s) = ", p); printf("%s \n", strrev3(p)); free(p); // ----------------------------------- // char s3[] = "oh! .. come on!!"; p = NULL; p = (char *) malloc(sizeof(char) * strlen("yet another version of strrev") + 1); strcpy(p, "yet another version of strrev"); printf("strrev4(%s) = ", s3); printf("%s \n", strrev4(s3)); printf("strrev4(%s) = ", p); printf("%s \n", strrev4(p)); free(p); return 0; }