我有这个string s1 = "My name is X Y Z"
,我想扭转这些词的顺序s1 = "Z Y X is name My"
.
我可以使用额外的数组来做到这一点.我认为很难,但有可能在现场(不使用额外的数据结构)并且时间复杂度为O(n)吗?
我目前正在通过KN King的C编程:现代方法.我已经超过了第8章(阵列)的文本,我很想转到第9章,但我还没有在每一章的最后解决所谓的"编程项目".不幸的是,第14个... 让我烦恼.
编写一个程序来反转句子中的单词.
Enter a sentence: you can cage a swallow can't you?
Reversal of sentence: you can't swallow a cage can you?
Run Code Online (Sandbox Code Playgroud)
提示:使用循环逐个读取字符并将它们存储在一维char数组中.让循环停止在一个句点,问号或感叹号("终止字符"),它保存在一个单独的char变量中.然后使用第二个循环向后搜索数组以查找最后一个单词的开头.打印最后一个单词,然后向后搜索倒数第二个单词.重复,直到到达数组的开头.最后,打印终止字符.
我一直在考虑将一个单词定义为空格之间的一系列字符.因此,当到达空间时,向后移动,打印每个字符,直到找到另一个空格.我的第一个版本的程序只打印了第一个单词.它的当前版本只打印其他单词.我已经坚持了两天,所以任何帮助都真的很感激.这是我的代码,以及输出示例.希望我已正确记录我的代码.提前致谢!
/* Include the standard I/O library */
#include<stdio.h>
/* Define main */
int main(void) {
/**
* Declare an array of characters storing the sentence, as well as
* a character representing the current character under cursor and
* the terminating …
Run Code Online (Sandbox Code Playgroud) 例如,如果我输入"这是一个字符串"我应该得到输出为"字符串a是这个"我不知道如何做到这一点
我有一个简单的函数,reverseWords(),它隐藏了字符串中的单词.例如.输入S ="这是一个字符串"给出"siht si a gnirts"的输出
我想知道这个功能的大O是什么.是O(N),O(N ^ 2)还是O(N*M)?
def reverseWords(S):
# code in Python 2.7
listA = S.split()
output = []
for element in listA:
output.append(element[::-1])
return (" ".join(output))
Run Code Online (Sandbox Code Playgroud)
是O(N),O(N ^ 2)还是O(N*M)?
我有一个名为str = "12345-5, 12345-4, 12345-3, 12345-2, 12345-1
我需要反转字符串的字符串,所以看起来str = "12345-1, 12345-2, 12345-3, 12345-4, 12345-5"
我已经尝试过 strReverse 方法,它几乎完成了我想要的......
Sub rev()
Dim str As String
str = "12345-5, 12345-4, 12345-3, 12345-2, 12345-1"
str = StrReverse(Trim(str))
'turns out to be str = "1-54321 ,2-54321 ,3-54321 ,4-54321 ,5-54321"
End Sub
Run Code Online (Sandbox Code Playgroud)
但它最终反转了整个字符串,应该猜到了。所以我想知道我是否应该使用正则表达式来解析字符串并删除“12345-”,然后将其反转并重新添加?我不太确定这是否是解决我的问题的最佳方法。有没有人知道我的问题的解决方案,或者可以指出我正确的方向?谢谢