新手简单字符串问题,求解答C语言

题目描述

一般的文本编辑器都有查找单词的功能,该功能可以快速定位特定单词在文章中的位置,有的还能统计出特定单词在文章中出现的次数。

现在,请你编程实现这一功能,具体要求是:给定一个单词,请你输出它在给定的文章中出现的次数和第一次出现的位置。注意:匹配单词时,不区分大小写,但要求完全匹配,即给定单词必须与文章

中的某一独立单词在不区分大小写的情况下完全相同(参见样例1 ),如果给定单词仅是文章中某一单词的一部分则不算匹配(参见样例2 )。

输入输出格式

输入格式:
输入文件名为stat.in ,2 行。

第1 行为一个字符串,其中只含字母,表示给定单词;

第2 行为一个字符串,其中只可能包含字母和空格,表示给定的文章。

输出格式:
输出文件名为stat.out 。

只有一行,如果在文章中找到给定单词则输出两个整数,两个整数之间用一个空格隔开,分别是单词在文章中出现的次数和第一次出现的位置(即在文章中第一次出现时,单词首字母在文章中的位置,位置从 0 开始);如果单词在文章中没有出现,则直接输出一个整数-1。

输入输出样例

输入样例#1:
To
to be or not to be is a question
输出样例#1:
2 0

输入样例#2:
to
Did the Ottoman Empire lose its power at that time
输出样例#2:
-1

我的代码,有错误,但不知到在哪。。。
#include
#include
int stricmp1(const char*word,const char*words);
int main()
{

char word[20];
char words[100];
int count = 0;
int turn =0;
int ture = 0;
int star;
scanf("%s",word);
getchar();
getchar();
while(1)
{

    scanf("%s",words);
    if(stricmp1(word,words)==0)
        count++;

    if(count == 1&&ture==0)
    {
        ture++;
        star = turn;
    }
    if(getchar() == ' ')
        continue;
    else
        break;
    turn++;
}
if(count)
    printf("%d %d",count,star);
else
    printf("-1\n");
return 0;

}
int stricmp1(const char*word,const char*words)
{
int back;
int j;
int count = 0;
int len1,len2;
len1 = strlen(word);
len2 = strlen(words);
if(len1==len2)
{
for(j=0;j<len1;j++)
{
if(word[j]==words[j])
count++;
else if(word[j]+32==words[j])
count++;
else if(word[j]==words[j]+32)
count++;
}
if(count==len1)
back = 0;
else
back = -1;
}
else
back = -1;
return back;
}

比较字符串为什么不直接用strcmp()?,比较之前把单词都先转换成小写再比较就可以了。

#include
#include
int main()
{
//freopen("text.in","r",stdin);
char word[20];
char words[200];
int judge = 0;
int ture = 0;
int count = 0;
int star;
int i;
int k;
gets(word);
gets(words);
int len1 = strlen(word);
int len2 = strlen(words);
for( i = 0;i<len2;i++)
{
// if(words[i]==' ')
// i++;
int j = i;
for( k=0;k<len1;k++,j++)
{
if(word[k]==words[j])
judge++;
else if(word[k]+32==words[j])
judge++;
else if(word[k]==words[j]+32)
judge++;
}
if(judge==len1)
{
count++;
if(ture==0)
{
ture++;
star=i;
}
}
k = 0;
judge = 0;
}
if(count)
printf("%d %d",count,star);
else
printf("%d",-1);
return 0;
}


http://blog.csdn.net/liangguojunainia/article/details/22757451

http://blog.sina.com.cn/s/blog_606a23dd01013zv7.html

#include
#include
#include

#define IN_FINE_NAME "d:\stat.in"
#define OUT_FINE_NAME "d:\stat.out"
#define DELIMITERS " "
#define INVALID -1

int main()
{
FILE *in;

FILE *out;

int i = 0;

int total_words = 0;

int key_words = 0;

int start_pos = 0;

int first_key = INVALID;

char key_word[32];

char context[1024];

char words[32][32];

char *tmp;

in = fopen(IN_FINE_NAME, "r");
if (in == NULL)
{
    printf("open file error:%d", errno);
    return -1;
}

/*读取一个字符串 */
fscanf(in, "%s", key_word);
/*读取字符串文本,第一行忽略 */
fgets(context, 1024, in);
fgets(context, 1024, in);

/*将字符串存放在words数组 */
tmp = strtok(context, DELIMITERS);
while (tmp)
{
    strcpy(words[total_words++], tmp);
    tmp = strtok(NULL, DELIMITERS);
}

strlwr(key_word);
for (i = 0; i < total_words; i++)
{
    strlwr(words[i]);
    if (0 == strcmp(key_word, words[i]))
    {
        if (first_key == INVALID)
        {
            first_key = i;
        }

        key_words++;
    }
}

for (i = 0; i < first_key; i++)
{
    start_pos += strlen(words[i]) + 1;
}

if (key_words == 0)
{
    key_words = -1;
}

out = fopen(OUT_FINE_NAME, "w");
if (out == NULL)
{
    printf("open out file error:%d", errno);
    return -1;
}
printf("%d  ", key_words);
printf("%d\n", start_pos);

fprintf(out, "%d  ", key_words);
fprintf(out, "%d\n", start_pos);

fclose(in);
fclose(out);

}

把你的稍微修改了下,也可以了
#include
#include
#include

#define IN_FINE_NAME "d:\stat.in"
#define OUT_FINE_NAME "d:\stat.out"

int main()
{
char word[20];

char words[200];

int judge = 0;

int ture = 0;

int count = 0;

int star;

int i;

int k;

int j;

int match = 0;

FILE *in;

FILE *out;


in = fopen(IN_FINE_NAME, "r");
if (in == NULL)
{
    printf("open file error:%d", errno);
    return -1;
}

/*读取一个字符串 */
fscanf(in, "%s", word);
/*读取字符串文本,第一行忽略 */
fgets(words, 200, in);
fgets(words, 200, in);

int len1 = strlen(word);

int len2 = strlen(words);

for (i = 0; i < len2; i++)
{

     /*第一个字符前没有空格,不需要处理*/
    /*找到空格*/
    if((i != 0)&&(words[i] != ' '))
    {
        continue;
    }

    /*跳过空格,找到首字母*/
    if(i!=0)
    {
        i++;
    }

    j = i;

    for (k = 0; k < len1; k++, j++)
    {
        if (word[k] == words[j])
            judge++;
        else if (word[k] + 32 == words[j])
            judge++;
        else if (word[k] == words[j] + 32)
            judge++;
        else
            break;
    }

    /*匹配成功,由于每次是从空格后的字母开始,也就是从单词的第一个字母开始匹配
      匹配成功后,要确定最后一个字符是字符串结尾*/
    if ((judge == len1) && (words[i + len1]== ' '))
    {
        count++;
        if (ture == 0)
        {
            ture++;
            star = i;
        }
    }
    k = 0;
    judge = 0;
}
if (count)
    printf("%d %d", count, star);
else
    printf("%d", -1);
return 0;

}

【题目】
统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符。
请注意,你可以假定字符串里不包括任何不可打印的字符。
【输出】
输入: "Hello, my name is John"
输出: 5
解释: 这里的单词是指连续的不是空格的字符,所以 "Hello," 算作 1 个单词。