字符串反转&单词反转-小米电视

方法一:先是全字符反转,然后再以空格为界定符反转单词。

#include<stdio.h>
#include<string.h>

string_change(char * p, int start, int end)   //字符反转
{
        int i,len;
        char temp;
        len = strlen(p);

while(start<=end)
        {
                temp = p[start];
                p[start] = p[end];
                p[end] = temp;
                start++;
                end--;
        }
}

int main(void)
{
        char a[20] = "abc def ghi ";
        int i, start, end, tag = 0;
        printf("%s\n",a);
        string_change(a, 0, strlen(a)-1);

for(i = 0; i <= strlen(a); i++)
        {
            if((a[i] == ‘ ‘ ||a[i] == NULL) && tag == 1)
            {
                string_change(a, start, end);
                tag = 0;
                continue;
            }

if(a[i] != ‘ ‘)
            {
               switch(tag)
                {
                    case 0:tag  = 1;   //单词开始
                              start = i;   //单词开始位置
                              break;
                    case 1:end = i;
                              break;
                }
            }

}
        printf("%s\n",a);
        return 0;
}

方法二:找到每个单词开始的位置,然后反转。【转载】

int ReverseWords(char *str,char *resultWords[])
{
   int wordStart = 0;                            /* 判断单词是否开始,注意初始化 */
   int wordCount = 0;                          /* 统计单词个数,注意初始化 */
   char *tempWords[256];                  /* 指针数组,用来存放每个单词的起始地址 */
   int i;                                           /* 循环控制变量,遍历原始字符串 */
   int j;                                         /* 循环控制变量,控制单词地址 */

for (i = 0; str[i] != ‘\0‘; i++)
   {
       if (str[i] == ‘ ‘)                        /* 如果当前字符为空格,则表示不是单词 */
       {
           wordStart = 0;                      /* 表示符置零 */
           str[i] = ‘\0‘;                      /* 将非字母全部填充为\0 */
       }
       else                                 /* 如果当前字符不是空格,但是前面一个字符是空格,表示单词开始 */
           if (wordStart == 0 )
           {
               wordStart = 1;                                                    /* 单词开始 */
               tempWords[wordCount++] = &str[i];                 /* 每个单词开始的字母的地址 */
           }
   }

for (j = 0; j < wordCount; j++)
   {
        resultWords[j] = tempWords[wordCount - 1 - j];                      /* 逆转,为了逆序输出 */
   }

return wordCount;                          /* 返回单词个数 */

}

int main(void)
{

char str[] = "I love China forever.";
   printf("");
   char *resultWords[256];                                                  /* 指针数组,用来存放结果单词的起始地址 */
   int wordCount = ReverseWords(str,resultWords);              /* 调用 */
   int i;
   for (i = 0; i < wordCount; i++)
   {
        printf("%s ",resultWords[i]);
   }

putchar(‘\n‘);
   return 0;

}

时间: 2025-01-05 01:30:40

字符串反转&单词反转-小米电视的相关文章

【LeetCode在线编程记录-1】字符串按单词反转

写在前面 LeetCode(地址:https://oj.leetcode.com/)是一个在线编程网站,题目经典,测试用例完备,共计157道算法类的题目.之后我会记录我的一些练习题目,有些答案是我自己原创的(说是原创,也很可能是之前在别的地方看到的而已),有些是从讨论区看到的,我都会明确标注出处. Reverse Words in a String Given an input string, reverse the string word by word. For example, Given

实现字符串中单词反转

????#include <stdio.h> int main() { char str[]="student a am i"; printf("%s\n",str); char *p,*q; char temp; p=q=str; while(*q!='\0') { q++; } q--; while(p<=q) { temp=*p; *p=*q; *q=temp; p++; q--; } //反转整个字符串 char *s; q=p=s=str

字符串反转的进一步应用----单词反转

字符串反转:如给定一字符串 good bye boy. 反转之后: .yob eyb doog 实现思路: 分别从第一个字符和最后一个字符,同时向中间遍历,交换遇到的每一个字符.JAVA实现代码如下:字符数组str存储待反转的字符串. private static void inverse(char[] str, int start, int end){ int i = start; int j = end; while(i < j){ char tmp = str[i]; str[i] = s

ZOJ 1151 Word Reversal反转单词 (string字符串处理)

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=151 For each list of words, output a line with each word reversed without changing the order of the words. This problem contains multiple test cases! The first line of a multiple input is

[算法] C# Revert 单词反转字符串[低时间复杂度]

无聊期间想起了一道字符串反转的问题. 大致要求输入"I am a good boy",输出"boy good a am I". 要求不能用已经封装好的方法实现.于是乎,我上网查了一下,基本都是用了封装后类库.于是我自己写了一个小算法,低时间复杂度高空间复杂度的算法. private string Revert(string str) { if (str.Length == 0) { return string.Empty; } string newStr = nul

剑指Offer41 反转单词顺序,单词字符顺序不变

1 /************************************************************************* 2 > File Name: 41_ReverseWords.c 3 > Author: Juntaran 4 > Mail: [email protected] 5 > Created Time: 2016年09月04日 星期日 16时18分34秒 6 **************************************

leetcode——Reverse Words in a String 旋转字符串中单词顺序(AC)

题目如下: Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". click to show clarification. Clarification: What constitutes a word? A sequence of non-space characters c

【C语言】写一个函数,实现字符串内单词逆序

//写一个函数,实现字符串内单词逆序 //比如student a am i.逆序后i am a student. #include <stdio.h> #include <string.h> #include <assert.h> void reverse_string(char *left, char *right) //连续的字符串逆序 { char temp; while (right > left) { temp = *left; *left = *rig

小米电视2S加量不加价,你还会买吗?

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" > 新发现(光山居士)7月16日,小米公司正式公布48英寸4K智能电视小米电视2S,售价2999元.小米电视2S家庭影院版配备6+2扬声器的Soundbar和全铝合金无线低音炮,售价3999元,7月28日将首次在小米网开放购买. 我们知道.小米电视作为小米