zoukankan      html  css  js  c++  java
  • 字符串操作

    字符串反转


    #include "stdafx.h"
    #include<iostream>
    using namespace std;
    char*reverse_str(char*str);
    int _tmain(int argc, _TCHAR* argv[])
    {
    	char*str = "abcdefgh";
    	char*dst = reverse_str(str);
    	cout << dst << endl;
    	system("pause");
    	return 0;
    }
    
    char*reverse_str(char*str)
    {
    	char*dst=new char[strlen(str)+1];
    	*dst = '';
    	
    	while (*str != '')
    	{
    		*(--dst) = *(str++);
    		cout << *dst << endl;
    	}
    	return dst;
    }


    atoi


    // my_atoi.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include<iostream>
    using namespace std;
    int my_atoi(char*in);
    int _tmain(int argc, _TCHAR* argv[])
    {
    	char*in = "0123h4dfdd";
    	cout << atoi(in) <<endl;
    	cout << my_atoi(in) << endl;
    	system("pause");
    	return 0;
    }
    
    int my_atoi(char*in)
    {
    	int kk = 0;
    	int sum = 0;
    	while (*in <= '9'&&*in >= '0')
    	{
    		int kk=*in-'0';
    		sum = sum*10+kk;
    		++in;
    	}
    	return sum;
    }


    itoa

    // my_itoa.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include<iostream>
    using namespace std;
    
    char*my_itoa(int value, char *string, int radix)
    {
    	int k = 0;
    	int m;
    	int a = value;
    	while (a > 0)
    	{
    		k++;
    		a = a / radix;
    	}
    	string[k] = '';
    	while (k > 0)
    	{
    		m = value%radix;
    		string[k - 1] = m + '0';
    		value = value / radix;
    		--k;
    	}
    	return string;
    }
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	char*str=new char[10000];
    	cout << my_itoa(132, str, 2) << endl;
    	system("pause");
    	return 0;
    }
    





    编写一个程序实现功能:将两个字符串合并为一个字符串并且输出,用指针实现。

    </pre><pre name="code" class="cpp">#include "stdafx.h"
    #include<iostream>
    using namespace std;
    char*my_strcat(char*src, char*dst)
    {
    	char*p = dst;
    	while (*p != '')
    		p++;
    	while (*src != '')
    	{
    		*p = *src;
    		p++;
    		src++;
    
    	}
    	*p = '';
    
    	return dst;
    
    }
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	char*src = new char[100];
    	strcpy(src, "RST");
    	char*dst = new char[100];
    	strcpy(dst, "ggggg");
    
    	cout << my_strcat(src, dst);
    	system("pause");
    	return 0;
    }
    


    子字符串的替换(C语言) 
    描述:编写一个字符串替换函数,如函数名为 StrReplace(char* strSrc, char* strFind, char* strReplace),strSrc为原字符串,strFind是待替换的字符串,strReplace为替换字符串。 
    举个直观的例子吧,如:“ABCDEFGHIJKLMNOPQRSTUVWXYZ”这个字符串,把其中的“RST”替换为“gggg”这个字符串,结果就变成了: ABCDEFGHIJKLMNOPQggggUVWXYZ 


    // StrReplace.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include<iostream>
    using namespace std;
    
    
    char*StrReplace(char* strSrc, char* strFind, char* strReplace)
    {
    	char*p1 = strSrc;
    	char*p2 = strFind;
    	char*p=NULL;
    	bool flag = false;
    	while (*p1 != '')
    	{
    		if (*p1 == *p2)
    		{
    			p = p1;
    			while (*p1 == *p2&&*p1 != '')
    			{
    				p1++;
    				p2++;
    				if (*p2 == '')
    				{
    					flag = true;
    					break;
    				}
    			}
    		}
    		else
    			p1++;
    		if (flag)
    			break;
    
    		}
    	if (flag)
    	{
    		char*p3=new char[100];
    		//char*p4 = p3;
    		char*p5 = p1;
    		//while (*p1 != '')
    		//	*(p3++) = *(p1++);
    		strcpy(p3, p1);
    		//*p3 = '';
    		
    		while (*strReplace != '')
    		{
    			*p = *strReplace;
    			p++;
    			strReplace++;
    		}
    		while (*p3 != '')
    		{
    			*(p) = *(p3);
    			++p;
    			++p3;
    		}
    		*p = '';
    	}
    	return strSrc;
    
    }
    
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	char*src = new char[100];
    	strcpy(src, "ABCDEFGHIJKLMNOPQRSTpVWXYZ");
    	//src = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";不能直接传const char*类型的值,否则在程序中无法改变值
    	char*find = new char[100];
    	//char*find = "RST";
    	strcpy(find, "RST");
    	char*replace = new char[100];
    	strcpy(replace, "ggggg");
    	//char*replace = "ggggggggg";
    	cout << StrReplace(src, find, replace) << endl;
    	system("pause");
    	return 0;
    }



    字符串分割

    int split(const std::string& str, std::vector<std::string>& ret_, std::string sep = ",")  
    {  
        if (str.empty())  
        {  
            return 0;  
        }  
      
        std::string tmp;  
        std::string::size_type pos_begin = str.find_first_not_of(sep);  
        std::string::size_type comma_pos = 0;  
      
        while (pos_begin != std::string::npos)  
        {  
            comma_pos = str.find(sep, pos_begin);  
            if (comma_pos != std::string::npos)  
            {  
                tmp = str.substr(pos_begin, comma_pos - pos_begin);  
                pos_begin = comma_pos + sep.length();  
            }  
            else  
            {  
                tmp = str.substr(pos_begin);  
                pos_begin = comma_pos;  
            }  
      
            if (!tmp.empty())  
            {  
                ret_.push_back(tmp);  
                tmp.clear();  
            }  
        }  
        return 0;  
    }


    sprintf

    字符串格式化命令,主要功能是把格式化的数据写入某个字符串中

    #include<stdio.h>/*某个stdio.h*/
     
    int main()/*主函数“整数”类型*/
    {
     char buffer[50];/*“字符”类型的数组,下面共有50个元素。*/
     int n,a=5,b=3;/*三个变量都为“整数”类型,int中间要有空格*/
    n=sprintf(buffer,"%d plus %d is %d",a,b,a+b);/*赋予数值*/
    printf("[%s]is a string %d chars long ",buffer,n);/*“格式输出函数”*/
    return 0;/*“返回零”
    也就是程序正常退出*/
    }
    输出结果:
    5 plus 3 is 8


    下面这个函数处理文本很有用

    fscanf


    #include <stdio.h>
    FILE *stream;
    int main(void)
    {
        long l;
        float fp;
        char s[81];
        char c;
        stream = fopen("fscanf.out", "w+");
        if(stream==NULL)
        printf("The file fscanf.out was not opened ");
        else
        {
            fprintf(stream,"%s%ld%f%c","a-string", 65000,3.14159, 'x');
            /*将指针设置至文件开头*/
            fseek(stream,0L,SEEK_SET);
            /*从文件中读取数据*/
            fscanf(stream,"%s",s);
            fscanf(stream,"%ld",&l);
            fscanf(stream,"%f",&fp);
            fscanf(stream,"%c",&c);
           /*输出读取的数据*/
            printf("%s ",s);
            printf("%ld ",l);
            printf("%f ",fp);
            printf("%c ",c);
            fclose(stream);
        }
        return 0;
    }




    版权声明:

  • 相关阅读:
    如何多个router 进行合并?
    钉钉微应用开发
    vscode 常用命令行
    window.location.search 为何在url 带# 号时获取不到 ?
    如何在嵌套的app中运用vue去写单页面H5
    两秒内不能重复点击
    linux系统下安装dubbo-admin
    二、SpringBoot实现上传文件到fastDFS文件服务器
    一、手把手教你docker搭建fastDFS文件上传下载服务器
    idea中git远程版本回退
  • 原文地址:https://www.cnblogs.com/walccott/p/4956907.html
Copyright © 2011-2022 走看看