字符串比较-----链表实现

用链表实现两个字符串的比较,相同则提示the same 并把他们打印出来(只用打印一个就行了);不同提示not the same,然后把两个不同的字符串分别打印出来。

源代码如下: VC6.0编译通过

#include <stdio.h>
#include <stdlib.h>
struct string{
  char   ch;
  struct string *next;
};

int string_cmp(struct string *string1, struct string *string2){

  struct string  *temp1, *temp2;

  temp1 = string1;
  temp2 = string2;

  while(temp1 != NULL){
   if(temp1->ch == temp2->ch){
    temp1 = temp1->next;
    temp2 = temp2->next;
   }
   else
    return 0;
  }

  return 1;
}

struct string *string_write(){
  
  char ch;
  struct string *str, *head, *tail;
  str = head = tail = NULL;
  printf("please input the chars end with enter/n");
  scanf("%c",&ch);
  while(ch != '/n'){
 
  str = (struct string*)malloc(sizeof(struct string));
  str->ch =ch;

  if(head == NULL){
   head = tail = str;
   head->next = tail->next = NULL;
  }

  tail->next = str;
  tail       = str;
  tail->next = NULL;

  scanf("%c",&ch);
  }
  
  return head;
}

void string_print(struct string *str){

  struct string *temp;
  temp = str;
  while(temp->next != NULL){
  
   printf("%c ",temp->ch);
   temp = temp->next;
  }
  printf("%c/n",temp->ch);
}

int main(){

 struct string *str1, *str2;

 str1 = string_write();
 str2 = string_write();

 if(string_cmp(str1, str2)){
  printf("these two strings are the same/n");
 
  string_print(str1);
 }
 
 else {
  printf("these two strings are not the same/n");
  string_print(str1);
  string_print(str2);
 }
 return 0;


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值