| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 698 人关注过本帖
标题:字符的处理
只看楼主 加入收藏
spawn0517
Rank: 1
等 级:新手上路
帖 子:5
专家分:1
注 册:2010-3-24
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:4 
字符的处理
求高手们帮忙!!急!急!急!
org.dat文件中按文本方式存放了一段其他文章,其中有若干长度小于15的英文单词,单词之间用空格分开,无其他符号。
(3)顺序读取这段文章的不同的单词(大小写敏感),同时在读取的过程中排除所有的单词THE以及变形,即这些单词不能出现在读取的结果中。
(4)将读取的所有单词的首字母转大写后,输出D根目录下new.txt,每个单词一行。
------------------------
那段文字可以点右键打开方式中用记事本打开,内容是:
The constructor is used to initialize the object The destructor is used to delete the Object the calling seqence of constructor is opposite to the calling sequence of destructor
-----------------
正确结果应该是
Constructor
Is
Used
To
Initialize
Object
Destructor
Delete
Object
Calling
Seqence
Of
Opposite
搜索更多相关主题的帖子: 字符 
2010-04-04 16:54
ltyjyufo
Rank: 9Rank: 9Rank: 9
来 自:未来
等 级:蜘蛛侠
威 望:2
帖 子:353
专家分:1166
注 册:2009-10-25
收藏
得分:6 
临时写的,没有去认真的优化一下,楼主慢慢看吧,需要改进的地方自己动动手吧。。。不好意思哦

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
    char temp[16],ch;
    int i=0;
    FILE *fp;
    printf("结果如下:\n");
    if((fp=fopen("D:\\org.dat","r"))==NULL)
    {
        printf("打开失败!!!\n");
        exit(1);
    }
    while(!feof(fp))//不是文件尾则继续循环
    {
       ch=fgetc(fp);
       for(i=0;ch!=' ';i++)//读取一个单词
       {
          temp[i]=ch;
          ch=fgetc(fp);
       }
       temp[i]='\0';
       if(!strcmp(temp,"THE")||!strcmp(temp,"THe")||!strcmp(temp,"The")||!strcmp(temp,"the")||!strcmp(temp,"thE")||!strcmp(temp,"tHE"))
       {//去掉The及其组合
           i=0;
           temp[i]='\0';
       }
       for(i=0;temp[i]!='\0';i++)//输出
          printf("%c",temp[i]);
       printf("\n");
    }
    fclose(fp);
}

[ 本帖最后由 ltyjyufo 于 2010-4-4 21:04 编辑 ]

翱翔天空的雄鹰固然令人羡慕,却容易被禁锢于牢笼之中,只有那夜色中的蝙蝠才是真正自由的飞翔者....
2010-04-04 21:01
ltyjyufo
Rank: 9Rank: 9Rank: 9
来 自:未来
等 级:蜘蛛侠
威 望:2
帖 子:353
专家分:1166
注 册:2009-10-25
收藏
得分:0 
如果对应的地方改一下就和你要求的结果一样了:(修改如下)
lab:while(!feof(fp))
    {
       ch=fgetc(fp);
       for(i=0;ch!=' ';i++)
       {
          temp[i]=ch;
          ch=fgetc(fp);
       }
       temp[i]='\0';
       if(!strcmp(temp,"THE")||!strcmp(temp,"THe")||!strcmp(temp,"The")||!strcmp(temp,"the")||!strcmp(temp,"thE")||!strcmp(temp,"tHE"))
       {
           i=0;
           temp[i]='\0';
           goto lab;
       }
 
   不提倡用这种方法

翱翔天空的雄鹰固然令人羡慕,却容易被禁锢于牢笼之中,只有那夜色中的蝙蝠才是真正自由的飞翔者....
2010-04-04 21:10
cnfarer
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:179
帖 子:3330
专家分:21157
注 册:2010-1-19
收藏
得分:6 
这个结果完全符合要求,只是省了读文件的过程!
程序代码:
#include <stdio.h>
#include <string>
int main(int argc, char *argv[])
{
    char s[]="The constructor is used to initialize the object The destructor is used to delete the Object the calling sequence of constructor is opposite to the calling sequence of destructor"; //省去读文件的过程
    int i=0,j=1,k=0,l=0;
    char c;
    FILE *fp=fopen("d:\\new.txt","w");
    char *p[100];
    while (s[i]!='\0')
    {
        if ((s[i]=='t' ||s[i]=='T' )&&(s[i+1]=='h' ||s[i+1]=='H' )&&(s[i+2]=='e' ||s[i+2]=='E'))
        {
                if (s[i+3]==' ')
                    {i+=4;}
                else
                    {i++;}
        }
        else if (s[i]==' ')
        {
            j=1;
            i++;
        }
        else
        {
            if (j==1)
            {
                p[k++]=new char(16);l=0;
            }
            c=j==1&&s[i]>='a'&&s[i]<='z'?s[i]-32:s[i];
            p[k-1][l]=c;l++;p[k-1][l]='\0';
            j=0;
            i++;
        }
    }
    for (i=0;i<k ;i++ )
    {
        for (j=i+1;j<k ;j++ )
        {
            if (strcmp(p[i],p[j])==0)
            {
                p[j][0]='\0';
            }
        }
    }
    for (i=0;i<k ;i++ )
    {
        if (p[i][0])
        {printf("%s\n",p[i]);fputs(p[i],fp);fputs("\n",fp);}
    }
    fclose(fp);
    return 0;
}


[ 本帖最后由 cnfarer 于 2010-4-5 07:31 编辑 ]

★★★★★为人民服务★★★★★
2010-04-04 21:28
u2jrmao
Rank: 2
等 级:论坛游民
帖 子:20
专家分:11
注 册:2010-3-25
收藏
得分:6 
路过!!!加油
2010-04-04 21:29
快速回复:字符的处理
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027064 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved