a code that rearranges alphabets entered using a pointer in ascending order of ASCII code

Asked 2 years ago, Updated 2 years ago, 53 views

I would like to sort the random strings of the alphabet you entered in ascending order of ASCII code and print them out.

example
Original string: bfGageGaheifhalenbcuafuhneixlDbfuhflfi0
After sorting: DGGaaaaaabbbceeeffffffffghhhhhhiiilllnnuux

Ends and prints when a number is entered.

I wrote the following myself, but only the last line feed is displayed in the output.
Please correct the code.

Additional
We have corrected the obvious mistake at this time.

You said the initialization of head.next=&tail; would not work, but how exactly should I fix it?

#include<stdio.h>
# include <stdlib.h>

structure ascii {
  int num;
  structure ascii* next;
};


int main() {
  inti;
  charc;
  structure ascii head, tail;
  head.next=&tail;
  tail.num = 10000;
  for(;(i=getchar())<48||i>57;){
    if(i==10){
      continue;
    } else {
    structure ascii*p=(struct ascii*) malloc(size of(struct ascii));
    p->num=i;
    structure ascii*w;
    for(w=&head;w->next!=&tail;){
    if(w->next->num<p->num){
      w = w - > next;
      if(w->next->num>=p->num){
    p->next=w->next;
        w->next=p;
    break;
      }
    } else {
      p->next=w->next;
    w->next=p;
    break;
    }
    }
    }
  }
  structure ascii*q;
  for(q=&head;q->next!=&tail;q=q->next){
    c=q->num;
    printf("%c",c);
  }
  printf("\n");
  return 0;
}

c pointer

2022-09-30 12:07

3 Answers


© 2024 OneMinuteCode. All rights reserved.