zoukankan      html  css  js  c++  java
  • 常用的字符串处理模型

    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    /*strstr_while字串模型*/
    int Search_count(char *p, char *str)
    {
    	int ncount = 0;
    
    	if (NULL == p || NULL == str)
    	{	
    		printf("func Search_count() err: %d (NULL == p || NULL = str)
    ", ncount);
    		return -1;
    	}
    	else
    	{
    		while (p = strstr(p, str))
    		{
    			ncount++;
    			p += sizeof(str);
    
    			if (*p == '')
    				break;
    		}
    		return ncount;
    	}
    }
    
    /*两头堵字串模型1*/
    int NoSpaceLen1(char *str, char *newStr)
    {
    	int i = 0, j = strlen(str) - 1;
    	int ncount = 0;
    
    	if (NULL == str || NULL == newStr)
    	{		
    		printf("func NoSpaceLen1() err:-1 (NULL == str)
    ");
    		return -1;
    	}
    
    	while (isspace(str[i]) && str[i] != '')
    		i++;
    	while (isspace(str[j]) && str[j] != '')
    		j--;
    
    	ncount = j - i + 1;
    	strncpy(newStr, str+i, ncount);  //将两头去除空格后的子串保存至newStr中
    	newStr[ncount] = '';          //此处需要特别注意,不能忘记加字串结尾标志
    
    	return ncount;
    }
    
    /*两头堵字串模型2*/
    //此函数需要将去除空格后的子串保存到原str中,所以要求其所指向的内存空间可以被修改才可以
    int NoSpaceLen2(char *str)
    {
    	int i = 0, j = strlen(str)-1;
    	int ncount = 0;
    
    	if (NULL == str)
    	{
    		printf("func NoSpaceLen2() err: -1 (NULL == str)!
    ");
    		return -1;
    	}
    	else
    	{
    		while (isspace(str[i]) && str[i] != '')
    			i++;
    		while (isspace(str[j]) && str[j] != '')
    			j--;
    
    		ncount = j - i + 1;
    		strncpy(str, str+i, ncount);  //将两头去除空格后的子串保存至原str中
    		str[ncount] = '';           //此处需要特别注意,不能忘记加字串结尾标志
    
    
    		return ncount;
    	}
    }
    
    /*字符串反转1*/
    //此函数需要将反转的字串保存到原str中,所以要求其所指向的内存空间可以被修改才可以
    int Inverse(char *str)
    {
    	int len = strlen(str);
    	char *p1, *p2;
    	char temp;
    
    	if (NULL == str)
    	{
    		printf("func Inverse() err: -1 (NULL == str)!
    ");
    		return -1;
    	}
    	else
    	{
    		p1 = str;
    		p2 = str + len -1;
    
    		while (p1 < p2)
    		{
    			temp = *p1;
    			*p1 = *p2;
    			*p2 = temp;
    			p1++;
    			p2--;
    		}
    
    		return 1;
    	}
    }
    
    
    
    int main()
    {
    	char *p = "54abcd12133abcd22abcd333333abcd";
    	char *s = ""; 
    	char *str = "    sdfssf   ";
    	char newStr[1024] = { 0 };
    	char s1[20] = "   99 ds  ";
    	char s2[20] = "  I love you !  ";
    	
    
    	if (Search_count(p, s) != -1)
    		printf("字符串%s中含有%d个字串%s!
    
    ", p, Search_count(p, s), s);
    
    	if (NoSpaceLen1(str, newStr) != -1)
    		printf("该字串去除前后空格后的字符数: ncount = %d  子串:%s
    
    ", NoSpaceLen1(str, newStr), newStr);
    
    	if (NoSpaceLen2(s1) != -1)
    		printf("该字串去除前后空格后的字符数: ncount = %d  字串:%s
    
    ", NoSpaceLen2(s1), s1);
    
    
    	Inverse(s2);
    	printf("%s
    %d
    ", s2);
    
    	return 0;
    }
    

      

  • 相关阅读:
    Linux input子系统学习总结(一)---- 三个重要的结构体
    DRM/KMS 基本组件介绍
    Framebuffer 驱动学习总结(二)---- Framebuffer模块初始化
    Framebuffer 驱动学习总结(一) ---- 总体架构及关键结构体
    Linux USB驱动学习总结(三)---- USB鼠标的加载、初始化和通信过程
    Linux USB驱动学习总结(一)---- USB基本概念及驱动架构
    使用Python调用动态库
    使用 SignalR与SSE(Sever sent event)向客户端推送提示信息
    在IDEA下使用Spring Boot的热加载(Hotswap)
    使用Spring boot + jQuery上传文件(kotlin)
  • 原文地址:https://www.cnblogs.com/enumhack/p/7472822.html
Copyright © 2011-2022 走看看