[不是问题]在字符串中交换单词 [英] [Not a question] swapping of words in a string

查看:96
本文介绍了[不是问题]在字符串中交换单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果输入是我爱印度

那么输出是I evoL aidnI
C编程中的

If the input is "I Love India"
then the output is "I evoL aidnI"
in C programming

推荐答案

参考此链接,在此之前付出努力学习。尝试通过引用一些文章来制定自己的逻辑和实现。



http://www.programmingsimplified.com/c/source-code/reverse-words-in-string [ ^ ]
Refer this link and before that put your effort in learning. Try to make your own logic and implement by referring some articles.

http://www.programmingsimplified.com/c/source-code/reverse-words-in-string[^]


如何使用 strcspn strlen
...
char str[] = "...";     // your line to transform inplace
const char sep[] = " "; // you name the separators here, many more?
int len = 0;
char *word = str;
while(NULL != (word = next_word(sep, word, &len))) swap_wordchars(word, len)
printf("%s", str);







int next_word(char *sep, char *word, int *len)
{
   // move forward behind the last match
   word += *len;
   // get length until separator or until end of text (try until no leading sep)
   while(0 == (*len = strcspn(word, sep)) && *word) word++;
   // no more data: NULL
   return *len ? word : NULL;
}







void swap_wordchars(char* str, int len)
{
   int sentry = len / 2;
   for(int i = 0; i < sentry; ++i) swap_chars(str+i, str+len-1-i);
}







void swap_chars(char *a, char *b)
{
    char c = *a;
    *a = *b;
    *b = c;
}





我现在做了一半的功课 - 你必须做一些锅炉板编码(添加适当的包括和主要功能),编译,链接,运行。

你可能需要向某人解释代码;-)



干杯

Andi



I did now half of your homework - you have to do some boiler plate coding (add appropriate includes and the main function), compile, link, run.
And you might need to explain the code to someone ;-)

Cheers
Andi


访问这里..



http://answers.yahoo.com/question/index?qid=20101213084823AA65Hpq [ ^ ]
visit here..

http://answers.yahoo.com/question/index?qid=20101213084823AA65Hpq[^]


这篇关于[不是问题]在字符串中交换单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆