Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
I need some help with the following C code.?
I am trying to do a string compare and swap the strings in order to print them in alphabetical order.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <math.h>
#include <string.h>
int main (void)
{
static char *x[2] = {"Hello",
"Bye" };
int i;
for (i = 0; i < 2; ++i)
printf ("%s\n", x[i]);
printf ("\n");
int n = 2;
reorder (n , x);
return 0;
}
void reorder(int n, char *x[])
{
char *temp;
int i, item;
for (item = 0; item < n - 1; ++item)
for (i = item + 1; i < n; ++i)
if (strcmp(x[item], x[i]) > 0) {
temp = x[item];
x[item] = x[i];
x[i] = temp;
printf(temp);
}
return;
}
4 Answers
- suittiLv 71 decade agoFavorite Answer
The real problem is printf(temp); This needs to be printf("%s\n", temp);
I've also added an "x" so you can tell when the original array is printed.
I've also added a prototype to get rid of a compiler warning.
I've also added explicit braces so that it's easier to edit later.
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <math.h>
#include <string.h>
void reorder (int n, char *x[]);
int
main (void)
{
static char *x[2] = { "Hello",
"Bye"
};
int i;
for (i = 0; i < 2; ++i) {
printf ("%s\n", x[i]);
}
printf ("x\n");
int n = 2;
reorder (n, x);
return 0;
}
void
reorder (int n, char *x[])
{
char *temp;
int i, item;
for (item = 0; item < n - 1; ++item) {
for (i = item + 1; i < n; ++i) {
if (strcmp (x[item], x[i]) > 0) {
temp = x[item];
x[item] = x[i];
x[i] = temp;
printf ("%s\n", temp);
}
}
}
return;
}
- Craig RLv 61 decade ago
It's amazing how wrong the other two answers are.
One said char * x[2] is an array of two characters. That's not right. It's an array of two pointers to a character. So you're fine there.
Another said your printf() statement was wrong but didn't point out that you're not really printing the reordered array, you're printing only when the values are exchanged, and then only the greater of the two strings.
So what you need to do is repeat the little loop that prints the array after you call reorder(). And remove the printf() statement in reorder(). Otherwise it looks fine.
- BillLv 41 decade ago
The problem is that you're treating Strings as objects, which they are not in C.
To C, a String is an array of chars terminated by null.
In you're code you say:
static char *x[2] = {"Hello", "Bye" };
which is an array with only enough room for two chars.
- Anonymous4 years ago
it is criminal. "output = a || b && c;" is reminiscent of "output = a != 0 || b != 0 && c != 0;". Your definition of substantial, even however, is against the regulation. substantial's return form might desire to be int. some suggestion: - in the adventure that your software is writing to a text textile bypass, it is substantial to print a terminating new line on the grounds that some implementations require it with a view to real acquire the line. (replace "printf("%d", output);" to "printf("%dn", output);") - circumvent utilising old libraries like conio. It replace into designed for DOS and is not any longer transportable to different structures. in case you fairly desire to verify C, follow usual libraries till you recognize extra effective. study to run your software today from cmd; getch is purely a crutch that prevents you from discovering the thank you to view the output of your courses wisely. @Dozo: in case you study C's priority regulations, you will locate that && easily has larger priority than ||. The expression is evaluated as "a || (b && c)". even however, on the grounds that each and each logical operator introduces a chain element, "a" is evaluated first, then "b && c" if "a" yields 0.