反转单词算法 c#

问题描述:输入一个单词字符串,如:"Hello, Nice to see you",不使用现有类库的情况下,实现输出如:"you see to Nice ,Hello"

算法简单描述:首先把字符串全部反转,然后按以空格为划分依据,对空格前的每个部分反转,从而复原单词,合成字符串输出

代码:

/// <summary>

/// 单词反转

/// </summary>

/// <param name="array">待反转的字符串</param>

/// <returns>已反转的字符串</returns>

/// <author>Max Su</author>

public static string ReverseWords(string array)

{

int arrLen = array.Length;

char[] strNew = new char[arrLen + 1];

 

//全部反转

for (int index = 0; index < arrLen; index++)

{

strNew[index] = array[arrLen - 1 - index];

}

strNew[arrLen] = ' '; //为了复原最后一个单词,添加一个辅助空格

 

int iStart = 0, iEnd = 0;

char tmpChar;

 

//复原单词

for (int Index = 0; Index < arrLen + 1; Index++)

{

//Index指向空格

if (strNew[Index] == ' ')

{

iEnd = Index - 1;

while (iStart < iEnd)

{

tmpChar = strNew[iStart];

strNew[iStart] = strNew[iEnd];

strNew[iEnd] = tmpChar;

iStart++;

iEnd--;

}

iStart = Index + 1;//跳过空格

}

else if (strNew[Index] == '!' | strNew[Index] == ',' | strNew[Index] == '.' | strNew[Index] == ';')

{

iStart = Index + 1;//跳过标点

}

}

 

//合成字符串

array = string.Empty;

for (int index = 0; index < strNew.Length - 1; index++ )//去掉辅助的空格

{

array += strNew[index];

}

return array;

}

转载于:https://www.cnblogs.com/max21011985/archive/2009/12/09/1620101.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 好的,以下是用C语言实现链表的代码示例: ```c #include <stdio.h> #include <stdlib.h> struct ListNode { int val; struct ListNode *next; }; struct ListNode* reverseList(struct ListNode* head) { struct ListNode* prev = NULL; struct ListNode* curr = head; struct ListNode* next = NULL; while (curr != NULL) { next = curr->next; curr->next = prev; prev = curr; curr = next; } return prev; } int main() { // 创建链表 1 -> 2 -> 3 -> 4 -> 5 struct ListNode* head = (struct ListNode*)malloc(sizeof(struct ListNode)); head->val = 1; head->next = (struct ListNode*)malloc(sizeof(struct ListNode)); head->next->val = 2; head->next->next = (struct ListNode*)malloc(sizeof(struct ListNode)); head->next->next->val = 3; head->next->next->next = (struct ListNode*)malloc(sizeof(struct ListNode)); head->next->next->next->val = 4; head->next->next->next->next = (struct ListNode*)malloc(sizeof(struct ListNode)); head->next->next->next->next->val = 5; head->next->next->next->next->next = NULL; // 输出原始链表 struct ListNode* node = head; while (node != NULL) { printf("%d ", node->val); node = node->next; } printf("\n"); // 链表 struct ListNode* newHead = reverseList(head); // 输出后的链表 node = newHead; while (node != NULL) { printf("%d ", node->val); node = node->next; } printf("\n"); return 0; } ``` 该代码中定义了一个链表结构体 ListNode,包含节点的值 val 和指向下一个节点的指针 next。实现了链表的函数 reverseList,采用迭代的方式,使用三个指针 prev、curr、next 分别表示前一个节点、当前节点和下一个节点,链表的过程就是不断地将 curr->next 指向 prev,然后更新三个指针的值。在 main 函数中创建了一个链表,然后输出原始链表和后的链表。 ### 回答2: 链表是一种常见的链表操作,可以使用C语言来实现。 首先,我们需要定义链表的结构体节点: ```c struct ListNode { int val; // 节点的数值 struct ListNode *next; // 指向下一个节点的指针 }; ``` 然后,我们可以定义一个函数来实现链表的: ```c struct ListNode* reverseList(struct ListNode* head) { struct ListNode *prev = NULL; // 指向前一个节点的指针 struct ListNode *curr = head; // 指向当前节点的指针 while (curr != NULL) { struct ListNode *nextNode = curr->next; // 暂存下一个节点的指针 curr->next = prev; // 将当前节点的指针指向前一个节点 prev = curr; // 更新前一个节点为当前节点 curr = nextNode; // 更新当前节点为下一个节点 } return prev; // 返回后的链表的头节点 } ``` 这个函数使用了迭代的方式进行链表。我们需要维护两个指针,一个指向前一个节点(初始为NULL),一个指向当前节点(初始为链表的头节点)。在遍历链表的过程中,每次都将当前节点的指针指向前一个节点,然后更新两个指针。最后,返回重置后的头节点。 如果要测试这个链表的算法,可以调用以下代码: ```c #include <stdio.h> int main() { // 创建链表 struct ListNode *node1 = (struct ListNode*)malloc(sizeof(struct ListNode)); struct ListNode *node2 = (struct ListNode*)malloc(sizeof(struct ListNode)); struct ListNode *node3 = (struct ListNode*)malloc(sizeof(struct ListNode)); node1->val = 1; node2->val = 2; node3->val = 3; node1->next = node2; node2->next = node3; node3->next = NULL; // 打印原链表 struct ListNode *curr = node1; while (curr != NULL) { printf("%d ", curr->val); curr = curr->next; } printf("\n"); // 链表 struct ListNode *reversedHead = reverseList(node1); // 打印后的链表 curr = reversedHead; while (curr != NULL) { printf("%d ", curr->val); curr = curr->next; } printf("\n"); return 0; } ``` 这段代码创建了一个包含三个节点的链表,并分别设置节点的值。然后,调用 `reverseList` 函数对链表进行,并打印后的链表的值。 这就是使用C语言实现链表算法的方法。 ### 回答3: 链表是将一个链表中的节点顺序进行的操作。通过C语言,我们可以使用指针来实现这个算法。 下面是用C语言实现链表算法的示例代码: ```c #include <stdio.h> #include <stdlib.h> struct Node{ int data; struct Node* next; }; struct Node* reverseList(struct Node* head){ struct Node* prev = NULL; struct Node* current = head; struct Node* next = NULL; while(current != NULL){ next = current->next; current->next = prev; prev = current; current = next; } head = prev; return head; } void printList(struct Node* head){ struct Node* temp = head; while(temp != NULL){ printf("%d ", temp->data); temp = temp->next; } printf("\n"); } int main(){ struct Node* head = NULL; struct Node* second = NULL; struct Node* third = NULL; // 分配内存 head = (struct Node*)malloc(sizeof(struct Node)); second = (struct Node*)malloc(sizeof(struct Node)); third = (struct Node*)malloc(sizeof(struct Node)); // 赋值 head->data = 1; head->next = second; second->data = 2; second->next = third; third->data = 3; third->next = NULL; printf("原始链表: "); printList(head); head = reverseList(head); printf("后的链表: "); printList(head); return 0; } ``` 以上是链表算法的实现。我们使用了三个指针,prev指向当前节点的前一个节点,current指向当前节点,next指向当前节点的下一个节点。通过不断更新这三个指针的指向,并将当前节点的next指向前一个节点,最后返回后的头节点,即可完成链表的操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值