嵌入式软件经典字符串面试、笔试题(基础)

平时自己用来温习的一些试题,如果有错误的地方,还望网友包涵和指正。

1.统计字符串中子字符串的个数。

#include 
#include 

int countCharNum(const char* FuString, const char* ZiString){
    int countString = 0;
    char* subString = ZiString;

    if(*ZiString == NULL){
        printf("Please Input Right Format SubString!\n");
        return 0;
    }
    while(*FuString != NULL){
        if(*FuString == *subString){
            subString++;
            if(*subString == NULL){//判断比较的子字符串是否结束
                countString++;//计数子字符串相等的个数
                subString = ZiString;
            }
        }else{
            subString = ZiString;//只要比较的字符不相等,就重置指针指向子字符串的开始位置
        }
        FuString++;//遍历整个字符串
    }
    return countString;
}

int main(int argc,char* argv[]){

    if(argc != 3){
        printf("                       __________________________________\n");
        printf("Please Input Format As |./countString  FuString  ZiString |\n");
        printf("                       ----------------------------------\n"); 
        return -1;
    }

    if(argc == 3){
        int stringNumber = 0;
        char* fuStr = argv[1];
        char* subStr = argv[2];

        stringNumber = countCharNum(fuStr,subStr);
        printf("The Number Of  %s(SubString) In %s(FuString) Is: %d\n", subStr, fuStr, stringNumber);

        return 0;
    }
}

测试结果如图:
嵌入式软件经典字符串面试、笔试题(基础)_第1张图片

2.基本数据类型所占用大小的问题

typedef struct hsmmc { 
        unsigned char res1[0x10];  
        unsigned int sysconfig;    
        unsigned int sysstatus;    
        unsigned char res2[0x14];  
        unsigned int con;   
        } hsmmc_t;  
        #define OMAP_HSMMC_BASE 0xe6000000  
        static hsmmc_t *mmc_base = (hsmmc_t *)OMAP_HSMMC_BASE;  

求&mmc_base-> sysstatus 的值?
某文Ku上的答案为0xe6000018,呵呵!!

#include 

#define OMAP_HSMMC_BASE 0xe6000000
typedef struct{
                unsigned char res1[0x10];//占用十六进制0x10个字节大小
                unsigned int sysconfig;//占用4个字节
                unsigned int sysstatus;
                unsigned char res2[0x14];
                unsigned int con;
        } hsmmc_t;
//声明一个hsmmc_t的结构体指针指向0xe6000000地址
static hsmmc_t* hsmmc = (hsmmc_t*) OMAP_HSMMC_BASE;

int main(int argc,char* argv[]){

    /*计算机环境不同可能有差异,我的测试环境是32位机*
     *char:占用1个字节 -128~127                *
     *unsigned char:占用1个字节 0~255          *
     *int:占用4个字节 -32768~32767             *
     *unsigned int:占用4个字节 0~65535          */

    printf("sizeof(int) is:%d\nsizeof(unsiged int) is:%d\nsizeof(char) is:%d\nsizeof(unsigned char) is:%d\n", sizeof(int), sizeof(unsigned int), sizeof(char), sizeof(unsigned char));

    printf("The Address of The struct hsmmc_t'sysstatus is: 0x%x\n", &hsmmc->sysstatus);

    return 0;

}

测试结果如图
嵌入式软件经典字符串面试、笔试题(基础)_第2张图片

3.倒油逻辑题

有三个油瓶,标号a,b,c,在装满时分别能盛7两、4两、3两油。现在a瓶中有7两油,假设在倒出倒入时油可以全部倒出。只使用三个瓶子,请倒出2两油。写出操作步(某题库没有作解答)
答案如图:
嵌入式软件经典字符串面试、笔试题(基础)_第3张图片

你可能感兴趣的:(C语言)