Storing an array in C - c

Context: I need to write a program that will accept inputs which will be stored into the array. Before storing in to the array, the inputted number must be checked if it already exists in the array or not. If it does not exist, it is stored into the array. If it exists, another input will be asked.
Now, my code will get inputs from the user, but the code will only work for the first input. It won't work for the second until the last input. Any pointers?
This is my code:
#include<stdio.h>
#define size 5
main()
{
int i;
arr[size];
input;
printf("This program will accept ");
printf("unique inputted numbers that will be stored");
printf(" in an array\n");
for(i = 0;i < size;i++)
{
printf("Enter input: ");
scanf("%d",&input);
if (unique(arr,input,i))
arr[i] = input;
else
i--;
//decrement i because ask for input again
}
for(i = 0;i < size;i++)
printf("%d ",arr[i]);
}
int unique(int arr[],int input,int i)
{
int n, z;
n = 0;
z = 1;
while(i > n)
{
if(arr[n] == input)
{
scanf("%d",&n);
z = 0;
break;
}
else
n=1;
break;
}
return z;
}

Your code is wrong at multiple levels:
The logic in the unique function is wrong.
Doing the scanf in the unique function is extremely bad design. The only thing unique should do is return 0 if input is already in the array.
You have used implicit variable declarations here: arr[size]; input;, it should be int arr[size]; int input;.
You should use descriptive variable names which makes your code easier to understand.
This is a working example (explanations in comments).
#include <stdio.h>
#define SIZE 5 // use capitals for macros (this is a convention)
int unique(int arr[], int value, int arrsize)
{
for (int i = 0; i < arrsize; i++)
{
if (arr[i] == value)
{
return 0; // value found in array
}
}
return 1; // value not found in array
}
void Test(int arr[], int arrsize, int value, int expected)
{
if (unique(arr, arrsize, value) != expected)
printf("Test failed for value %d\n", value);
}
void runtests()
{
int arr[] = { 1,2,3 };
Test(arr, 4, sizeof(arr) / sizeof(*arr), 1);
Test(arr, 1, sizeof(arr) / sizeof(*arr), 0);
Test(arr, 3, sizeof(arr) / sizeof(*arr), 0);
}
#define size 5
int main()
{
int i;
int arr[size]; // declare int variable
int input; // declare int variable
printf("This program will accept unique inputted numbers that will be stored in an array\n");
for (i = 0; i < size; i++)
{
printf("Enter input %d: ", i + 1);
scanf("%d", &input);
if (unique(arr, input, i)) // value already in the array?
arr[i] = input; // no => put it there
else
{ // yes => ask again
printf(" >> %d is already in the array\n");
i--;
}
}
for (i = 0; i < size; i++)
printf("%d ", arr[i]);
}
There are two more functions Test and runtests in this code. They are not called by this code, but they can be very useful for debugging. As an exercise try to understand why they can be useful during the debug phase of your code.

You're close, but overcomplicating it slightly.
Let's take a step back and think about this at a high level. You want to store unique inputs in the array, up to the size of the array. In pseudocode:
while array not full
prompt for and read next input
if input not already in array
store input
else
write a message
end if
end while
What's really key is that you only need one input statement - your unique function should only check for the presence of the input value in the array and return true or false. It shouldn't do any input of its own.
So your main loop is more like
while ( i < size )
{
fputs( "Gimme a number: ", stdout );
/**
* Break out of the loop if there's an error
* on input.
*/
if ( scanf( "%d", &input ) != 1 )
break;
if ( unique( arr, i, input ) )
arr[i++] = input;
else
printf( "%d already exists in the array, try again.\n", input );
}
All your unique function needs to do is cycle through the elements of the array. By calling unique with i instead of size it will only check array elements that have been written to so far and not bother with unassigned elements. This way you don't have to make sure that all of the array elements have been initialized to some known, out-of-band value that's guaranteed to compare unequal to any valid input.
You'll need to compile against C99 or later and include stdbool.h to use the bool type and the true and false constants.
#include <stdbool.h>
...
bool unique( int *arr, size_t size, int input )
{
bool result = true;
for( size_t i = 0; i < size && result; i++ )
if ( arr[i] == input )
result = false;
return result;
}
If you want to get really terse, you could directly assign the result of the Boolean expression to result:
for ( size_t i = 0; i < size && result; i++ )
result = (arr[i] == input);
but people will hit you. It's perfectly valid code, but a little eye-stabby, and most programmers aren't used to seeing Boolean expressions outside of an if, for, while, or switch statement control expression.
Finally, some suggestions:
Fix your formatting. The compiler doesn't care, but it makes it easier for other people to understand what you're trying to do and to spot mistakes.
The presence of main() in your code suggests you're using C89 or K&R C. C99 did away with implicit int declarations. You really should define main as either int main( void ) or int main( int argc, char **argv ). Furthermore, you should move to a compiler that supports later versions of C (C11 or C18).

Related

How to break a while loop when it is false to a certain condition

I was trying to make a program where if I enter an integer, the program would find out the bigger number and subtract it by the smaller number. This part, I got it.
The problem is, the infinite loop part.
I tried to get type in two integers keep on printing with the while loop, and break when at least one character is typed in.
For example, if I type in 2 #, it would break.
But I couldn't find the write place to get the break; within the code and therefore whenever I enter a character it would keep on creating an infinite loop.
Is there any way to create a break in this code? I humbly ask for advice...
The following is the code which I couldn't put the break
(By the way, the reason I did the condition in while as sizeof(i)==4 || sizeof(j)==4 was to make it so it would only enter an integer, since the size of an integer is 4)
int main()
{
int i, j;
int result;
while (sizeof(i)==4 || sizeof(j)==4){
printf("type in two integers : ");
scanf("%d %d", &i, &j);
if (i < j) {
result = j - i;
}
else if (j < i){
result = i - j;
}
printf("%d\n", result);
}
return 0;
}
The bottom code is the one I tried to put break but failed (it kept creating an infinite loop)...
int main()
{
int i, j;
int result;
while (sizeof(i)==4 || sizeof(j)==4){
if (sizeof(i) == 4 || sizeof(j) == 4) {
printf("type in two integers : ");
scanf("%d %d", &i, &j);
if (i < j) {
result = j - i;
}
else if (j < i) {
result = i - j;
}
printf("%d\n", result);
}
else
break;
}
return 0;
}
and here's a code where I got rid of the sizeof and used while(1), though there wasn't much change in the fact that the break didn't work...
int main()
{
int i, j;
int result;
while (1){
printf("type in two integers : ");
scanf("%d %d", &i, &j);
if (i < j) {
result = j - i;
}
else if (j < i) {
result = i - j;
}
printf("%d\n", result);
}
return 0;
}
You can't use sizeof(i) to do run-time checks! This is a compile-time constant that, in your case (32-bit integers) will always evaluate to 4.
In order to check that two valid integers have been given, you can check the return value of the scanf function (it gives the number of fields successfully scanned):
#include <stdio.h>
int main()
{
int i, j;
int result;
while (1) {
printf("type in two integers : ");
if (scanf("%d %d", &i, &j) != 2) break; // Break here if we didn't get two integers
if (i < j) {
result = j - i;
}
else if (j < i) {
result = i - j;
}
printf("%d\n", result);
}
return 0;
}
Feel free to ask fir further clarification and/or explanation.
Drop the whole concept of endless loop with break inside if.
Make a condition for the loop based on the return value of scanf(), that is practically what it is designed for.
#include <stdio.h>
int main()
{
/* always init everything */
int i=0, j=0;
int result=0;
printf("type in two integers : ");
while (2==scanf("%d %d", &i, &j))
{
if (i < j) {
result = j - i;
}
else /* removed second if, to have a meaningful result for i==j */
{
result = i - j;
}
printf("%d\n", result);
printf("type in two integers : ");
}
return 0;
}
I'd probably actually use do {...} while (...) with a variable storing the return value of scanf()for being used in the loop condition. I'd consider it more elegant for not having to copy the print, but I kept it closer to your code structure.
More comments on your code:
as explained in comments, sizeof() works differently than you seem to think; it is static and does not change at runtime and hence cannot be used in a loop condition
with while (sizeof(i)==4 || sizeof(j)==4){if (sizeof(i) == 4 || sizeof(j) == 4){/* a */} else {/* b */}, b cannot ever be reached, because the conditions of while and if are identical
check the possible outcomes of the if conditions inside the loop, you are leaving the one with i==j undefined and return an uninitialised value
always init all variables as a habit
for a good MRE include the include lines
On your request, here is a proposal for the do-while alternative:
#include <stdio.h>
int main()
{
/* always init everything */
int i=0, j=0;
int result=0;
int iScanned=0;
do
{
printf("type in two integers : ");
iScanned=scanf("%d %d", &i, &j); /* keep the return value for loop */
if (i < j) {
result = j - i;
}
else /* removed second if, to have a meaningful result for i==j */
{
result = i - j;
}
if(2==iScanned) printf("%d\n", result); /* if to avoid awkward last output */
} while (2==iScanned);
return 0;
}

ADD elements of an array with a given condition

There is an array of n students( stu[n]).If gender is boy then my code adds
for boy b, 2nd,4th,6th,........even position elements of array and
for girl g, 1st,3rd,5th....odd position elements of array.
1> Gender of boys denoted by b.
2> Gender of girls denoted by g.
Input info>>
The 1st line contains n, denoting the number of students in the class, hence the number of elements in the array.
Each of the subsequent lines contains the marks of n students .
The last line contains gender b/g;
Output info>>
The output should contain the sum of all the alternate elements of marks as explained above.
#include <stdio.h>
int main() {
int n,i;
scanf("%d",&n);//n denotes number of students.
int stu[n],sum=0;
for(i=1;i<=n;++i)
scanf("%d",&stu[i]);//for accepting input in array.
char gen;
scanf("%s",&gen);//for accepting input gender b/g.
for(i=1;i<=n;i++){
if(gen=='g' && i%2!=0){ //girl g adds alternate odd position elements.
sum=sum+stu[i];
printf("%d",sum);
}
else if(gen=='b' && i%2==0){ //boy b adds alternate even position elements.
sum=sum+stu[i];
printf("%d",sum);
}
}
//code
return 0;
}
Sample Input
3
3
2
5
b
Sample Output
8
explanation>>
marks=[3,2,5] and gender = b so it will add 3+5(even position 0,2 alternate elements). If gender in place of b is g then it will produce output = 2.
My code is shows output of 0 in all test cases.
You have the major problem in
int n,i;
int stu[n],sum=0;
here, n being a uninitialized local scoped variable with automatic storage, the initial value is indeterminate.
Now, since the address of the variable was never taken and it has a type that can have trap representation, attempt to use the value (int stu[n]) will invoke undefined behavior.
You need to scan in the value into n first, then use that to define the VLA stu. Something like
int n,i;
scanf("%d",&n);//n denotes number of students.
// **Note: please check for errors in input with scanf return value.**
int stu[n],sum=0; // here, n has the scanned value.
That said,
char gen;
scanf("%s",&gen);
is also horribly wrong, you want to scan in a char, not a string, and with the address of a plain char variable, %s conversion specification would be UB, again. You should use %c and discard any whitespaces which is present in buffer altogether.
You're making things more complicated than they need to be. Here is how you can possibly do:
#include <stdio.h>
int main(void)
{
int mark;
int b = 0;
int g = 0;
char students_marks[5];
for (int i=0; i<5; i++) {
scanf("%d", &mark);
students_marks[i] = mark;
}
for (int i=0; i<5; i++) {
if (i%2 == 0) b += students_marks[i];
if (i%2 == 1) g += students_marks[i];
}
printf("Boys: %d\nGirls: %d\n", b, g);
return 0;
}
You should probably not use an array, and just ignore the first data point. It is (probably) easier to use a linked list. Or maybe just use two lists, alternating the inputs between them. And I would definitely not use scanf. If you are new to C, do NOT waste your time learning the foibles of the scanf format string language. The only time scanf is ever useful is in introductory courses where instructors incorrectly believe that you will be able to get input more quickly than if you spend time learning other methods. But in fact you will end up burning more time learning when to use spaces in the format string that you saved by not learning fread. After your introduction to C, you will (almost) never use scanf. Also, it seems like a horrible design to put the discriminant at the end of the input. The values to be summed (the gender) should be given as a command line argument. That said, you could just do:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
FILE *
xfopen(const char *path, const char *mode)
{
FILE *rv;
if( (rv = fopen(path, mode)) == NULL ) {
perror(path);
exit(EXIT_FAILURE);
}
return rv;
}
int
main(int argc, char **argv)
{
int i;
int size; /* Should probably be size_t. Skipping that for now. */
FILE *in = argc > 1 ? xfopen(argv[1], "r") : stdin;
int sum = 0;
if( fscanf(in, "%d", &size) != 1 || size <= 0 ) {
fprintf( stderr, "invalid input\n" );
exit(EXIT_FAILURE);
}
int grades[size];
for( i = 0; i < size; i++ ) {
if(fscanf(in, "%d", grades + i) != 1) {
fprintf( stderr, "invalid input on line %d\n", i );
exit(EXIT_FAILURE);
}
}
char gender;
if(fscanf(in, " %c", &gender) != 1) {
fprintf( stderr, "invalid input on line %d\n", i );
exit(EXIT_FAILURE);
}
if(strchr("bBgG", gender) == NULL) {
fprintf( stderr, "invalid gender: %c\n", gender);
exit(EXIT_FAILURE);
}
for( i = tolower(gender) == 'b'; i < size; i += 2 ) {
sum += grades[i];
}
printf("sum: %d\n", sum);
}
Hmm… i changed your code a Little bit and hope this runs as described.
int main() {
int n, index, sum=0;
int* stu;
scanf("%d", &n); // input number of studens
if(n>0)
stu = malloc(n*sizeof(int)); // allocate memory
else
return 0;
for(index=0;index<n;index++)
scanf("%d", &stu[index]);
char gen;
scanf("%c", &gen);
for(index=0; index<n; index++){
if(gen=='g' && index%2!=0) {
sum += stu[index];
printf("%d", sum);
} else if(gen=='b' && index%2==0) {
sum += stu[index];
printf("%d", sum);
}
}
return 0;
}

Doesn't work function free and code always crashed

My code crash coz free() doesn't want to clear array. First of all i create array and add to there a numbers. After that i enter K (k < m) and creates new matrix MxK. k - amount of number of first array in line. And if the number are over , should fill in the rest matrix with zeros. I use while for reuse, but my code crash when i use big array and big matrix. With what it can be connected?
`int main() {
while(1){
int m,k;
printf("Enter size of array: ");
int res;
do
{
res=scanf("%d",&m);
if(res!=1)printf("it should be a number!");
}
while(res!=1);
int * a = (int*) malloc(m*sizeof(int));
int i;
for (i = 0; i < m; i++)
{
printf("a[%d] = ", i);
do
{
res=scanf("%d", &a[i]);
flush();
if(res!=1) printf("it should be a number!\n");
}
while(res!=1);
}
for (i = 0; i < m; i++)
printf("%d ", a[i]);
printf("\nEnter K: ");
do
{
res=scanf("%d",&k);
flush();
if(res!=1 || k>m) printf("k<m must be or enter number\n");
}
while(res!=1 || k>m);
int all=k*m;
for(i=m;i<all;i++)
{
a[i] = 0;
}
int j;
int n=0;
int s[i][j];
for(i=0;i<m;i++)
{
for(j=0;j<k;j++)
{
s[i][j] = a[n];
printf("%d ",s[i][j]);
n++;
}
printf( "\n" );
}
for(i=0;i<m;i++)
{
for(j=0;j<k;j++)
{
free(s[i][j]);
}
}
int povtor; ----- exit using break
char temp[10];
printf("Continue?(1 - No, everything - Yes): ");
gets(temp);
povtor = atoi(temp);
if (povtor == 1 ) break;
}
return 0;
}`
Your code has a lot of problems.
First, you can only free something that was allocated with malloc, calloc, or realloc. This doesn’t apply to s[i][j].
But that’s only one issue. You allocate a to hold m elements:
int * a = (int*) malloc(m*sizeof(int));
but then you try to write past the end of the array here:
int all=k*m;
for(i=m;i<all;i++)
{
a[i] = 0;
}
You need to resize a before attempting that, otherwise you are writing over memory you don’t own:
int *tmp = realloc(a, sizeof *a * all );
if ( !tmp )
{
// resize failed, exit with error
}
a = tmp;
for( i = m; i < all; i++ )
{
a[i] = 0;
}
Then there’s this:
int j;
int n=0;
int s[i][j];
What is the value of j at this point? For that matter, what is the value of i? Are you sure you don’t mean s[m][k]?
You are trying to free an int, and not a pointer : int s[i][j] is an array of array of int, hence s[][] is a int.
Even though, s is automatic memory allocated : basically, its a variable stored in the stack so you don't need to free it. Trying to free static variable results in crash. This question may help you.
By the way, you have a lot of uninitialized variable, resulting in conditionals jumps and possible crashs : for example, you declare int j without a value and after you use it to declare your array of array.
Finally, don't use gets. This function is dangerous, use fgets instead. I suggest you read the manual page of gets to understand why.

Load Array Elements Using Function

I am trying to load elements into an array using a function but I don't know what I'm doing wrong. I want to load elements from a data file until -1 is entered. Here is what I have and I don't know what to do from here.
#include <stdio.h>
/*Function to scan in grades*/
int LoadArray (int grade[ ])
{
int i = 0;
while(scanf("%i", grade[i]) != -1) {
i++;
}
return i;
}
/*Main program*/
int main (void)
{
int grade[200], count=0;
/*Call function*/
count = LoadArray(grade);
printf("%i", count);
return 0;
}
Two problems.
1) You're missing an ampersand:
scanf("%i", &grade[i]);
you need to pass the address of the variable scanf should put the result in. You were passing the contents of grade[i] instead, and reading uninitialized memory is undefined behavior.
2) scanf doesn't return the value it read from stdin; you should compare the variable you read to -1. Also, it's bad practice to pass an array to a function but not its size, as then the function has no way of knowing how big the array is.
To sum up, the code with fixes looks like this:
/*Function to scan in grades*/
int LoadArray(int grade[], size_t gradeMaxCount)
{
int i = 0;
while (1)
{
int tmp;
if (i >= gradeMaxCount || scanf("%i", &tmp) != 1 || tmp == -1)
break;
grade[i++] = tmp;
}
return i;
}
Call the function like this:
count = LoadArray(grade, sizeof(grade) / sizeof(grade[0]));
Note that funny things will happen if i somehow becomes negative. You could make it size_t instead (this would also require you to change the function return type and print it using %zu), depending on how much of a purist you are.
int LoadArray (int grade[ ])
{
int i = 0;
for(int j=0;j<200;j++){
if((scanf("%d", grade+j) == -1) || (grade[j]==-1))
break;
i++;
}
return i;
}

Anagram project

I am having trouble trying to get the "isZero" function to detect if the word is an anagram or not. Asking if "isZero" is equal to 1 in main() it will only give me "anagram". And if i set it to 0 it will only give me "not anagram". To me it is not computing anything, it is just printing out whatever statement is true at the moment. Not sure how to solve this and could use some guidance.
#include <stdio.h>
#include <ctype.h>
#define MAX 26
void intialize(char a[], char b[], int c[]);
void setLetters(char newCount[], int newNumber[]);
void checkLetters(char b[], int newNumber[]);
int isZero(int c[]);
void getstring(char a[]);
void getString(char b[]);
int main(void)
{
char a[MAX], b[MAX];
int c[MAX];
intialize( a, b, c);
getstring(a);
getString(b);
setLetters(a, c);
checkLetters(b, c);
if (isZero(c) == 1) {
printf("anagram");
} else
printf("not anagram");
return 0;
}
void intialize(char a[], char b[], int c[])
{
int i;
for(i = 0; i < MAX; ++i) {
a[i] = '\0';
b[i] = '\0';
c[i] = 0;
}
}
void setLetters(char newCount[], int newNumber[])
{
int i, index = 0;
for(i = 0; i < MAX; ++i) {
if(isalpha(newCount[i])) {
newCount[i] = tolower(newCount[i]);
index = (int)(newCount[i] - 'a');
newNumber[index] +=1;
}
}
}
void checkLetters(char b[], int newNumber[])
{
int i, index;
for(i = 0; i < MAX; ++i) {
if(isalpha(newNumber[i])) {
newNumber[i] = tolower(newNumber[i]);
index = (int)(newNumber[i] - 'a');
newNumber[index] -= 1;
}
}
}
int isZero(int c[])
{
int i, j = 0;
for(i = 0; i < MAX; ++i) {
if(c[i] == 0)
j = 1;
else
return 0;
}
return j;
}
void getstring(char a[])
{
char line[MAX];
printf("Enter a string: ");
gets(line);
}
void getString(char b[])
{
char line[MAX];
printf("Enter a string: ");
gets(line);
}
With C it is mandatory that you slow down and understand what each part of each line does. There is no part of C where close enough is correct. That being said, you had an overall idea about how to approach the problem. However, it was very clear that you are just starting out in C (given the other answers and comments).
Before you start writing functions, determine what you need a function to do. Then try and determine how best to handle that task. If you need a function to get string input, then write one to do that. If you find yourself having to write one function to fill each string stop, your have just defeated the purpose of the function. Writing a function to do the same thing for a[] and another identical function for b[] makes no sense. You don't need a function to loop through all arrays setting your newly declared arrays to zero/NULL, that's what array initialization syntax is for.
Before you expect to have functions work, take the time to learn how to pass values to (and if a return is needed -- get values from) a function. When you pass an array to a function, pointer decay occurs. That means array a[] decays to *a when passed to a function. You can take advantage of this by declaring your functions to accept *a as the argument. While this isn't earth-shattering for a simple 1-D array, the decay becomes more involved with 2-D arrays and above.
In addition to figuring out which code makes sense as a function, you need to be just as exact with your logic in C as you are with its syntax. If there is any part of a line your are unsure about, look it up, look at the man page for the function you are using, or consult a language reference for your compiler (Gnu/MS), etc. to make sure you know exactly what your code does. It will save you time in the long run. The number one thing that trips new C programmers up, is trying to skim the manual or skim the book and then start writing code. The key to learning C is to just slow down.
Never, Never, Never use gets(). If you are taking a class and the teacher hands you an assignment using it, go to administration and ask for a refund. gets() is no longer part of the standard C library due to how easily it is compromised and exploited. Use fgets, getline, or scanf (read the entire section on proper use of the scanf format string if you chose to use it). It is a fine function, but it has many, many pitfalls just waiting for someone that partially understands its use.
That being said, you had the overall logic for one approach to anagrams. Below I've provided an example of the points above in sorting out your code. Take the time to read through it and understand why I made the changes I did. Additionally, I added a quick length check for the words input. If they are not the same length, no need to go further. Let me know if you have questions. There are a lot of good folks here that are happy to help.
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define MAX 26
void setLetters(char *newCount, int *newNumber);
void checkLetters(char *newCount, int *newNumber);
int isZero (int *c);
void getstring (char *a);
int main (void)
{
char a[MAX] = {0}; /* initialize all to zero/NULL */
char b[MAX] = {0};
int c[MAX] = {0};
getstring(a);
getstring(b);
printf ("\n You entered:\n\n a: %s\n b: %s\n\n", a, b);
/* test lengths - if differ, not anagram */
if (strlen(a) != strlen(b)) {
printf (" Lenghts differ, cannot be an anagram.\n\n");
return 1;
}
setLetters (a, c); /* set key array (c) */
checkLetters (b, c); /* check key array (c) */
if (isZero(c))
printf(" The words form an anagram.\n\n");
else
printf(" The words are not and anagram.\n\n");
return 0;
}
void setLetters (char *newCount, int *newNumber)
{
int i = 0;
int index = 0;
for (i = 0; i < MAX; ++i) {
if (isalpha (newCount[i])) {
newCount[i] = tolower (newCount[i]);
index = (int)(newCount[i] - 'a');
newNumber[index] +=1;
}
}
}
void checkLetters(char *newCount, int *newNumber)
{
int i = 0;
int index = 0;
for (i = 0; i < MAX; ++i) {
if (isalpha (newCount[i])) {
newCount[i] = tolower (newCount[i]);
index = (int)(newCount[i] - 'a');
newNumber[index] -= 1;
}
}
}
int isZero (int *c)
{
int i = 0;
for (i = 0; i < MAX; ++i)
if (c[i] == 1)
return 0;
return 1;
}
void getstring (char *a)
{
printf ("\n Enter a string: ");
scanf ("%[^\n]%*c", a);
}
output:
$ ./bin/anaproj
Enter a string: yekcim
Enter a string: mickey
You entered:
a: yekcim
b: mickey
The words form an anagram.
$ ./bin/anaproj
Enter a string: yekcim
Enter a string: mickez
You entered:
a: yekcim
b: mickez
The words are not and anagram.
void getstring(char a[]);
This API is not doing what you intend to do.
This has a local variable line and your are reading a string to it and the char array in main() i.e. a is never been filled up with anything.
You continue to use the char array a thinking the values are filled in it by calling getstring() which is not happening. You need to fix this first and later work on the algorithm for anagram.
There is something called pass by reference which might help you.

Resources