Robert Ryan wrote:
> I did a swap problem and got stuck at the end
>
> #include <stdio.h>
>
> void swap(char*, char*);
>
> void order_chars(char [], int);
>
> int main()
>
> {
>
> char g[3], a, z;
>
> a = 'A';
>
> z = 'Z';
What made you decide to use variables for single character storage? Not
necessary.
> printf("Enter three characters: \n");
You only have enough allocated space for two characters and a null
terminator.
> scanf("%s", &g);
Please stop using dangerous function calls.
> order_chars(g, 3);
>
> if(g[0] < a || g[3] > z) {
I have no idea what you are hoping to accomplish there but it is wrong.
You are stepping outside the bounds of the 'g' array.
> printf("Please enter in capital characters\n");
>
> } else {
>
> printf("%s\n", g);
>
> }
>
> }
>
>
>
> void order_chars(char a[], int n)
>
> {
>
> int i, j;
>
> for(i=0; i<n-1; ++i)
>
> for(j=n; i<j; --j)
>
> if
> (a[i-1] > a[i])
>
> swap(&a[i-1], &a[i]);
>
> }
>
>
>
> void swap(char *a, char*b)
>
> {
>
> char temp;
>
> temp = *a;
>
> *a = *b;
>
> *b = temp;
>
> }
Indentation needs work but is a lot better than before.
Why there is a space between every line of code above could be a
copy-and-paste e-mail client issue but it does makes the code hard to read.
--
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197
*NEW* MyTaskFocus 1.1
Get on task. Stay on task.
http://www.CubicleSoft.com/MyTaskFocus/