以单词为单位翻转一个字符串
2012-08-19 21:33
295 查看
1. 以单词为单位翻转一个字符串;
input: To be or not to be, this is a question
ouput: question a is this, be to not or be To
解题思路:
* 如何界定一个单词; 是用空格来界定;
* 采用堆栈的数据结构,将单词一个个压入后再弹出即可;
* 堆栈的结点定义要求数据为字串;
* 其中关关键之处在于如何将字串分解为单词,存入在数组中
在内存中字串的存放形式如下:
To\20be\20or\20not\20to\20be,\20this\20is\20a\20question\0
要求分解后为:char* word[]={"To","be","or","not","to","be","this","is","a","question"}
这样处理之后,反转的方式就可以有多样的选择了;
首先可以看到分解单词的界定符为\20,如保要判断到一个\20字符之后将单词存入数组?
用伪程序表示如下:
pString = "To be or not to be,this is a question!"
header = pString
if(pString[i] == \20)
pString[i] = \0
word[count] = header
header += i;
count++;
运行结果如下,可以看出,最后一个单词的处理还不OK,不过留到明天处理:
input: To be or not to be, this is a question
ouput: question a is this, be to not or be To
解题思路:
* 如何界定一个单词; 是用空格来界定;
* 采用堆栈的数据结构,将单词一个个压入后再弹出即可;
* 堆栈的结点定义要求数据为字串;
* 其中关关键之处在于如何将字串分解为单词,存入在数组中
在内存中字串的存放形式如下:
To\20be\20or\20not\20to\20be,\20this\20is\20a\20question\0
要求分解后为:char* word[]={"To","be","or","not","to","be","this","is","a","question"}
这样处理之后,反转的方式就可以有多样的选择了;
首先可以看到分解单词的界定符为\20,如保要判断到一个\20字符之后将单词存入数组?
用伪程序表示如下:
pString = "To be or not to be,this is a question!"
header = pString
if(pString[i] == \20)
pString[i] = \0
word[count] = header
header += i;
count++;
/* Copyright (C) * 2012 - peng.wu * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * */ #include "stack.h" int count_word(char* string) { int i=0,count=0; if(string == NULL) return 0; while(string[i]) { if(string[i] == 0x20) { count++; } i++; } count+=1; return count; } char* word[30]; char** split_string(char* string) { int i=0,count=0; char* header; char* buffer; buffer=malloc(100); memset(buffer,0x0,100); strcpy(buffer,string); header = buffer; if(string == NULL) return 0; while(buffer[i]) { if(buffer[i] == 0x20) { buffer[i] = '\0'; word[count] = header; printf("%s\n",word[count]); header = header+i+1; buffer = header; count++; i=0; continue; } if(buffer[i]=='\0') { word[count]=buffer; printf("%s\n",word[count]); } i++; } return ; } int main(int argc,char* argv[]) { char* string="To be or not to be, this is a question!"; int count=0; int i; count=count_word(string); printf("word count is :%d\n",count); split_string(string); return 1; }
运行结果如下,可以看出,最后一个单词的处理还不OK,不过留到明天处理:
相关文章推荐
- 每天一道LeetCode-----以单词为单位逆序字符串,每个单词之间以一个空格分隔(原字符串中可能有多个空格)
- Java 写一个方法判断一个字符串是否对称 "asdfgasdfg"、编写一个程序,将下面的一段文本中的各个单词的字母顺序翻转,
- 翻转子串 假定我们都知道非常高效的算法来检查一个单词是否为其他字符串的子串。请将这个算法编写成一个函数,给定两个字符串s1和s2,请编写代码检查s2是否为s1旋转而成,要求只能调用一次检查子串的函数。
- 以单词为单位的翻转字符串
- 【我解C语言面试题系列】013 以单词为单位的翻转字符串
- 给定一个字符串,逐个翻转字符串中的每个单词
- 以单词为单位的翻转字符串
- 【我解C语言面试题系列】013 以单词为单位的翻转字符串
- JAVA用标准库自己写一个字符串翻转方法,翻转字符串中字母非单词
- 以单词为单位倒序输出一个字符串
- 以单词为单位倒序输出一个字符串
- 以单词为单位对字符串进行翻转(用C#编写,但不用其中的库函数)
- Reverse Words in a String 翻转一个字符串里的单词顺序 @LeetCode
- 以单词为最小单位翻转字符串
- 实现以单词为单位的翻转字符串
- C/C++面试之算法系列--以单词为最小单位翻转字符串
- 字符串单词翻转---程序库---C
- 翻转字符串中单词的顺序(腾讯面试)
- 实现一个函数翻转字符串(将一个字符串的内容,逆序存储)
- 字符串之“统计一个字符串中单词的个数”