1442: Neo 的简单字符串(字符串)

1442: Neo 的简单字符串

时间限制: 10 Sec 内存限制: 128 MB

提交: 9 解决: 3 统计

题目描述

Neo 给你一系列字符串,请你输出字符串中的不同单词个数以及总单词个数。

输入

多组输入,每组数据都是一行字符串(长度小于200),其中每个单词以空格隔开(单词都是小写字母组成)。

输出

输出字符串中的不同单词个数以及总单词个数。

样例输入

i love china
aa aa bb

样例输出

3 3
2 3

来源

提交讨论
#include<bits/stdc++.h>
#define M 300

using namespace std;

int main()
{
    char str[M][M];
    int num, ans, a;
    char b[M], c;
    
    while(scanf("%s%c", str[0], &c) != EOF)
    {
        num = 1;
        a = 1;
        
        while(c != '
')
        {    
            /* 输入第二个字符,然后与前面的字符进行比较 */
            scanf("%s%c", b, &c);
            bool same = false;
            for ( int i = 0; i < num; i++)
            {
                ans = strcmp(str[i], b);
                /* 如果两个字符串相同,字符串总数减一 */
                if( ans == 0 )
                {
                    same = true;
                    break;
                }    
            }
            
            if( !same )
            {
                strcpy(str[num++], b);
            }
            a++;
        }
        
        printf("%d %d
", num, a);    
    
    }

    return 0;
}
View Code

收获:

1.C/C++函数,比较两个字符串:
设这两个字符串为str1,str2,
若str1==str2,则返回零;
若str1<str2,则返回负数;
若str1>str2,则返回正数。
matlab中函数,strcmp(s1,s2) 判断两个字符串s1和s2是否相同,相同返回true ,不同返回false
 
2.strcpy是一种C语言的标准库函数,strcpy把从src地址开始且含有''结束符的字符串复制到以dest开始的地址空间,返回值的类型为char*。
原型声明:char *strcpy(char* dest, const char *src);
头文件:#include <string.h> 和 #include <stdio.h>
功能:把从src地址开始且含有NULL结束符的字符串复制到以dest开始的地址空间
说明:src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
返回指向dest的指针
 
永远渴望,大智若愚(stay hungry, stay foolish)
原文地址:https://www.cnblogs.com/h-hkai/p/7955313.html