Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <string.h>
- typedef struct node
- {
- char c;
- struct node *next;
- }*NodePtr;
- void printlist(NodePtr list)
- {
- NodePtr temp = list;
- do{
- printf("%c", temp->c);
- temp=temp->next;
- }while(temp!=NULL);
- printf("\n");
- }
- NodePtr newNode(char str)
- {
- NodePtr np = (NodePtr) malloc(sizeof (NodePtr));
- np -> c = str;
- np -> next = NULL;
- return np;
- }
- void replace(NodePtr list, char c1, char c2)
- {
- while(list){
- if(list->c == c1 )
- list->c = c2;
- list=list->next;
- }
- }
- NodePtr removenode(NodePtr list, char c3)
- {
- NodePtr temp =list, prev = NULL;
- while(temp!=NULL){
- if(temp->c == c3 )
- {
- if(list == temp ){
- list=temp->next;
- free(temp);
- temp=list;
- }else
- {
- prev->next= temp->next;
- free(temp);
- temp= prev->next;
- }
- }
- else
- {
- prev = temp;
- temp= temp->next;
- }
- }
- //printlist(list);
- return list;
- }
- void toUpper(NodePtr list)
- {
- while(list)
- {
- list->c = toupper(list->c);
- list = list->next;
- }
- }
- void toLower(NodePtr list)
- {
- while(list)
- {
- list->c = tolower(list->c);
- list = list->next;
- }
- }
- NodePtr alfaSort(NodePtr list)
- {
- NodePtr bfrFirst=NULL, First=NULL, prev = NULL, temp =list;
- char FirstChar = '~'; //karena memiliki nilai terbesar di ASCII Table
- if(list->next==NULL)
- return list;
- while(temp)
- {
- if(temp->c < FirstChar){
- FirstChar = temp->c;
- First = temp;
- if(prev !=NULL)
- bfrFirst= prev;
- }
- prev= temp;
- temp= temp->next;
- }
- if(bfrFirst!=NULL)
- bfrFirst->next= First->next;
- if(First!=list)
- First->next = list;
- list=First;
- list->next = alfaSort(list->next);
- return list;
- }
- int compare(NodePtr list, NodePtr cmp)
- {
- NodePtr temp = list;
- NodePtr Banding = cmp;
- int tanda = 0;
- while(temp){
- // printf("0");
- if(temp->c == Banding->c)
- {
- tanda=1;
- if(Banding->next== NULL) return 1;
- else {
- temp=temp->next;
- Banding=Banding->next;
- // printf("1");
- }
- // printf("2");
- } else temp = temp->next;
- }
- if (tanda == 0) return 0;
- }
- int main(){
- char str[100],c1,c2;
- int n=0;
- NodePtr head =NULL, tail, temp, sorted;
- printf("masukkan Kalimat: ");
- gets(str);
- while(str[n]!='\0')
- {
- temp= newNode(str[n]);
- if(head==NULL) head=temp;
- else
- tail->next = temp;
- tail = temp;
- n++;
- }
- //printlist(head);
- // -------------------fungsi E -----------------------
- printf("Kalimat yang akan dibandingkan: ");
- gets(str);
- NodePtr cmp=NULL, ctemp,ctail;
- n=0;
- while(str[n]!='\0')
- {
- ctemp= newNode(str[n]);
- if(cmp==NULL) cmp=ctemp;
- else
- ctail->next = ctemp;
- ctail = ctemp;
- n++;
- }
- if(compare(head, cmp)==1) printf("Sama\n");
- else printf("Beda\n");
- // -------------------fungsi A-----------------------
- printf("huruf yang ingin di ganti dan penggantinya: ");
- scanf("%c %c",&c1, &c2);
- replace(head,c1,c2);
- printlist(head);
- getchar();
- // -------------------fungsi B -----------------------
- printf("huruf yang ingin dihapus: ");
- scanf("%c",&c1);
- head = remove(head,c1);
- printlist(head);
- // -------------------fungsi C -----------------------
- printf("Huruf Kapitalis: ");
- toUpper(head);
- printlist(head);
- // -------------------fungsi D -----------------------
- printf("Huruf yang Terurut: ");
- sorted = head;
- toLower(sorted);
- alfaSort(sorted);
- //getchar();
- printlist(sorted);
- //printlist(cmp);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement