praveenmhapsekar
BAN USERAnother C version
#include <stdio.h>
char STRING[] = {"dog and cat bites man and woman"};
void
reverse(char *str)
{
char *tStr;
int count = 0;
if (*str == '\0') // end of string; Recursion base case
return;
tStr = str; // make copy of pointer to traverse string
while((*tStr != ' ') && (*tStr != '\0')) {
// traverse till next word or end of the string
tStr++;
count++;
}
tStr++; // Skip pass ' ' [white space]
reverse(tStr); // recursion, repeat same process for rest of the string.
printf("%.*s ", count, str);
#if 0
// Another way to print string
int i = 0;
while (count) {
printf("%c", str[i++]);
count--;
}
printf("%c", ' ');
#endif
}
int
main() {
printf("%s\n", STRING);
reverse(STRING);
printf("\n");
return 0;
}
output
dog and cat bites man and woman
woman and man bites cat and dog
C Version
/*
* Given an array of positive integers and a target total of X, find if there exists a contiguous subarray with sum = X
*
* [1, 3, 5, 18]
* X = 8 Output: True
* X = 9 Output: True
* X = 10 Output: False
* X = 40 Output :False
*/
#include <stdio.h>
#define SIZE 4
#define VERDICT(_X_) printf("X = %3d Output = %s\n", _X_, (findSubArray(_X_) ? "True" : "False"))
static int A[SIZE] = {1,3,5,18};
int
findSubArray(int X)
{
int sum;
int i = 0; // Array iterator
int j = 0; // SubArray iterator
for (i = 0; i < SIZE; i++) {
j = i;
sum = 0;
while ((X > sum) && (j < SIZE)) {
sum = sum + A[j++];
if (sum == X) {
return 1; // Found contiguous sub-array sum = X
}
}
}
return 0; // contiguous sub-array not found.
}
int
main()
{
VERDICT(1);
VERDICT(4);
VERDICT(8);
VERDICT(19);
VERDICT(21);
VERDICT(22);
VERDICT(23);
VERDICT(26);
VERDICT(27);
return 0;
}
output for test data
X = 1 Output = True
X = 4 Output = True
X = 8 Output = True
X = 19 Output = False
X = 21 Output = False
X = 22 Output = False
X = 23 Output = True
X = 26 Output = True
X = 27 Output = True
C Program :
#include <stdio.h>
/*
* power
* 0 -->
* 1 --> Tens (0-19, special handling)
* 2 --> Hundred
* 3 --> Thousand
* 4 --> Tens Thousand (0-19, special handling)
* 5 --> Hunderd Thousand
* 6 --> Miliion
* 7 --> Tens Million (0-19, special handling)
* 8 --> Hunderes Millions
* 9 --> Billion
* 10 --> Tens Billion (0-19, special handling)
* 11 --> Hundreds Billion
*/
const char *ZEROTOTEEN[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "forteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
const char *TENS[] = {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
const char *BASE[] = {"", "", "hundred ", "thousand ", "", "", "million ", "", "", "billion "};
int carry = 0;
void
printnum(int n, int power)
{
int tempNum;
power++;
if (n <= 0) {
return;
} else {
printnum(n/10, power);
tempNum = n % 10;
switch (power) {
case 1: // tens
case 4: // ten thousand
case 7: // ten million
case 10: // ten billion
switch (tempNum) {
case 0:
carry = 0;
break;
case 1:
carry = 10;
break;
default:
printf("%s ", TENS[tempNum]);
break;
}
break;
case 5: // hundred thousand
case 8: // hundred million
case 11: // hundred Billion
power = 2; // hundred
default: // case 0 [0-19], case 2 [hundred], 3 [thousand], 6 [million], 9 [billion],
if ((tempNum + carry )!= 0) {
printf("%s %s", ZEROTOTEEN[tempNum + carry], BASE[power]);
carry = 0;
}
break;
}
}
}
#define MAX_NUM 21
int main ()
{
// Test data
int n[MAX_NUM] = {0, 1, 10, 11, 99, 100, 101, 110, 113, 123, 190, 999, 1000, 1245, 9999, 10000, 10200, 913913, 999999, 1000000, 1234567890};
int i;
// Test loop
for (i = 0; i < MAX_NUM; i++) {
printf("\n%13d\t", n[i]);
if (n[i] == 0) {
printf("zero"); // Handle special case of zero
} else {
printnum(n[i], -1); // Call print function
}
}
printf("\n");
return 0;
}
Output:
0 zero
1 one
10 ten
11 eleven
99 ninety nine
100 one hundred
101 one hundred one
110 one hundred ten
113 one hundred thirteen
123 one hundred twenty three
190 one hundred ninety
999 nine hundred ninety nine
1000 one thousand
1245 one thousand two hundred forty five
9999 nine thousand nine hundred ninety nine
10000 ten thousand
10200 ten thousand two hundred
913913 nine hundred thirteen thousand nine hundred thirteen
999999 nine hundred ninety nine thousand nine hundred ninety nine
1000000 one million
1234567890 one billion two hundred thirty four million five hundred sixty seven thousand eight hundred ninety
C Program
output:
- praveenmhapsekar September 21, 2016