void f(char *s)
{
if(*s)
{
f(s+1)
putchar(*s)
}
}
main()
{
char s[100]
printf("请输入一个单词:")
gets(s)
f(s)
printf("\n")
}
#include <stdio.h>#include<string.h>
int main()
{
char a[100]
int k,i,j,p
gets(a)
k=strlen(a)
j=k
for(i=k-1i>=0i--)
{
if(a[i]==' ')
{
p=i//先保存当前要输出的位置
while( a[i-1] == ' ' ) i-- //跳过多余的空格
for(p<jp++)
{
printf("%c",a[p+1])//输出单词中的各字母
}
j=i //记录下当前结束位置
}
}
for(i=0i<ji++)
{
printf("%c",a[i])
}
printf("\n") //换行
return 0
}
****************************FileName: WordCount.cpp
Author: cox Version : 1.0 Date: 2007/1/12
Description: 文件中指定单词个数的统计
Version: 1.0
Function List:
1. TransformFile()// 将文件转换为有效的格式
2. CountWord() // 单词计数
3. IsValidChar() // 检查是否有效的英语字母
4. GetInput() // 从输入流读取数据
5. UpperCase() // 字符强制转换为大写
History:
<author> <time> <version > <desc>
cox2007/1/12 1.0 build this moudle
***************************************************/
#include <iostream>
#include <fstream>
#include <string>
using namespace std
#include "linklist\\linklist.h"
bool TransformFile( char* inFile, char* outFile )
template <typename datatype>
bool CountWord( char* filename, LinkList<datatype>&list )
bool IsValidChar( char ch )
template <typename datatype>
bool GetInput( LinkList<datatype>&list )
bool UpperCase( string &str )
/*************************************************
Function: main()
Description:主函数
Calls:链表类, TransformFile(), GetInput(),
CountWord()
Called By: 无
Table Accessed: 无
Table Updated: 无
Input: 无
Output: 结果
Return: 0
Others: 无
*************************************************/
int main()
{
LinkList<string>wordList
TransformFile( "test1.txt", "test1.tmp" )
if ( !GetInput( wordList ) )
{
cout <<"Bad input!!" <<endl
}
if ( !CountWord( "test1.tmp", wordList ) )
{
cout <<"An error accord dring count!!" <<endl
}
return 0
}
/*************************************************
Function: TransformFile()
Description:将文件去掉标点符号, 并按照单词以空格分组
Calls: IsValidChar()
Called By: main()
Table Accessed: 无
Table Updated: 无
Input: char* inFile // 输入文件名
char* outFile// 输出文件名
Output: 状态
Return: 操作是否成功
Others: 无
*************************************************/
bool TransformFile( char* inFile, char* outFile )
{
ofstream outFileStream( outFile, ios::out )
ifstream inFileStream ( inFile , ios::in )
if ( !outFileStream || !inFileStream )
{
cout <<"Open files error!!" <<endl
return false
}
char readChar = '\0'
cout <<"Transfroming..."
// 转换
while ( inFileStream.get(readChar) )
{
if ( IsValidChar(readChar) )
{
outFileStream <<readChar
}
else
{
outFileStream <<" "
}
}
cout <<"OK\n"
outFileStream.close()
inFileStream.close()
return true
}
/*************************************************
Function: IsValidChar()
Description:检查是否有效的英语字符
Calls: 无
Called By: TransformFile()
Table Accessed: 无
Table Updated: 无
Input: char ch// 待检查的字符
Output: 无
Return: true为是
Others: 无
*************************************************/
bool IsValidChar( char ch )
{
if ( (ch >= 65 &&ch <= 90) || (ch >= 97 &&ch <= 122) )
{
return true
}
else
{
return false
}
}
/*************************************************
Function: GetInput()
Description:读取输入流并存入链表中
Calls: 链表类
Called By: main()
Table Accessed: 无
Table Updated: 无
Input: LinkList<datatype>&list// 储存输入数据的链表
Output: 无
Return: 操作是否成功, true为成功
Others: 无
*************************************************/
template <typename datatype>
bool GetInput( LinkList<datatype>&list )
{
string strInput = ""
cout <<"Input your words to count, split by \',\': "
cin >>strInput
string tmpString = ""
strInput += ","
for ( int index = 0index <strInput.size()index++ )
{
if ( strInput[index] == ',' )
{
list.Insert(tmpString)
tmpString = ""
}
else
{
tmpString += strInput[index]
}
}
return true
}
/*************************************************
Function: CountWord()
Description:单词计数
Calls: 链表类, UpperCase()
Called By: main()
Table Accessed: 无
Table Updated: 无
Input: char* filename // 待计数的文件
LinkList<datatype>&list// 储存输入数据的链表
Output: 无
Return: 操作是否成功, true为成功
Others: 无
*************************************************/
template <typename datatype>
bool CountWord( char* filename, LinkList<datatype>&list )
{
ifstream inFileStream ( filename , ios::in )
int lenth = list.Lenth()
int *counts = new int [lenth]
for ( int clrIndex = 0clrIndex <list.Lenth()clrIndex++ )
{
counts[clrIndex] = 0
}
string tmpString
string tmpRead
while ( inFileStream >>tmpRead )
{
for ( int index = 0index <list.Lenth()index++ )
{
list.Locate(index+1)
tmpString = list.Peek()
UpperCase( tmpString )
UpperCase( tmpRead )
if ( tmpString == tmpRead )
{
counts[index]++
}
}
}
for ( int index = 0index <lenthindex++ )
{
list.Locate(index+1)
tmpString = list.Peek()
cout <<tmpString <<" = " <<counts[index] <<endl
}
cout <<endl
inFileStream.close()
return true
}
/*************************************************
Function: UpperCase()
Description:强制转换成大写
Calls: 无
Called By: CountWord()
Table Accessed: 无
Table Updated: 无
Input: string &str // 待转换的字符
Output: 无
Return: 操作是否成功, true为成功
Others: 无
*************************************************/
bool UpperCase( string &str )
{
for ( int index = 0index <str.size()index++ )
{
if ( str[index] >96 )
{
str[index] -= 32
}
}
return true
}
需要链表,自己写一个吧,大一就该学了的~~