Anagrams and Numeric Bases Assignment Sample
Your task is to write 2 programs, the first is to find anagrams and the second is to convert values on different bases. You should read from stdin, and find anagrams of the first word read in. Given the input acts, facts, rats, cats it should output acts, cats. It should be case insensitive and only consider alphabetic characters. For the second program, read in a base followed by a series of numbers. Allow any base from 2 to 36 (using 0-9, a-z for the digits). If a nonvalid digit is detected report the error to stderr, otherwise output the value to stdout. For more C programming assignments contact us for a quote.
Solution:
Anagrams.c
#include
#include
#include
#include
char input[66], sorted[65], givin[66] , given_sorted[66] ;
void swap(char *a , char *b)
{
char tmp = *a ;
*a = *b ;
*b = tmp;
}
void sort(char *s)
{
// function to sort the string and make all letters in lower case
int len = strlen(s);
if (len == 0)
{
exit(0);
}
for(int i =0;i
{
s[i] |= (1<<5);
for(int j = i; j
{
if (s[i] > s[j])
{
swap(&s[i], &s[j]);
}
}
}
}
bool isAlphabet(char c)
{
return ( (c >= ‘a’ && c <= ‘z’) || (c >= ‘A’ && c <= ‘Z’) ) ;
}
bool isOkString(char *s)
{
int len = strlen(s);
for(int i =0;i
{
if(isAlphabet(s[i])==0)
return 0;
}
return 1 ;
}
void check()
{
if (isOkString(givin)){
strcpy(given_sorted, givin);
sort(given_sorted);
if(strcmp(input , given_sorted) == 0)
{
printf(“%s\n”, givin);
}
}
else
puts(“Bad Input … non-alphabetical character\n”);
}
void inputCheck()
{
if (isOkString(input)==0)
{
puts(“Bad first string”);
exit(1);
}
if (strlen(input))
printf(“%s\n”, input);
sort(input);
}
int main()
{
scanf(“%s”, input);
inputCheck();
while(1){
scanf(“%s”, givin);
check();
memset(givin, 0 , sizeof given_sorted);
}
return 0;
}
ChangeBase.c
#include
#include
#include
#include
#include
int given_base;
unsigned long output ;
char input[6];
unsigned long numbers[6];
bool isAlphabet(char c)
{
return ((c >= ‘a’ && c <= ‘z’) || (c >= ‘A’ && c <= ‘Z’));
}
int convertToNumbers(char c)
{
if (isAlphabet(c))
{
return (c-‘a’ + 10) ;
}
return c-‘0’;
}
void lowerCaseIinput()
{
int len = strlen(input);
for(int i = 0; i < len ; i++)
{
if (isAlphabet(input[i]))
{
input[i] |= (1<<5); // convert it to lower case
}
}
}
void getNumbers()
{
memset(numbers, 0, sizeof(numbers));
int len = strlen(input);
for (int i = 0; i < len; i++)
{
numbers[i] = convertToNumbers(input[i]);
}
}
bool isLegalBase()
{
int len = strlen(input);
for(int i = 0; i < len; i++)
{
if (numbers[i] >= given_base)
{
return 0;
}
}
return 1;
}
unsigned long power(unsigned long x, unsigned long y)
{
if (y == 0)
return 1;
else if (y % 2 == 0)
return power(x, y / 2) * power(x, y / 2);
else
return x * power(x, y / 2) * power(x, y / 2);
}
void fillOutput()
{
int len = strlen(input);
int j = 0;
for(int i = len-1; i >= 0; i–)
{
output += power(given_base, j) * numbers[i];
j++;
}
}
void run()
{
if (strlen(input) == 0)
{
exit(0);
}
lowerCaseIinput();
getNumbers();
if (isLegalBase()==0)
{
printf(“Not a %d base number\n” , given_base);
return ;
}
output = 0;
fillOutput();
printf(“%lu\n”, output);
}
int main()
{
scanf(“%d”, &given_base);
if ( (given_base >1 && given_base <37) == 0)
{
puts(“Bad value for base.”);
exit(1);
}
while(1){
memset(input, 0 , sizeof input);
scanf(“%6s”, input);
run();
int len = strlen(input);
// for(int i =0;i
// printf(“%lu “, numbers[i]);
puts(“”);
}
return 0;
}