字符串操作

字符串反转


#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 = '\0';
	
	while (*str != '\0')
	{
		*(--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] = '\0';
	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 != '\0')
		p++;
	while (*src != '\0')
	{
		*p = *src;
		p++;
		src++;

	}
	*p = '\0';

	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 != '\0')
	{
		if (*p1 == *p2)
		{
			p = p1;
			while (*p1 == *p2&&*p1 != '\0')
			{
				p1++;
				p2++;
				if (*p2 == '\0')
				{
					flag = true;
					break;
				}
			}
		}
		else
			p1++;
		if (flag)
			break;

		}
	if (flag)
	{
		char*p3=new char[100];
		//char*p4 = p3;
		char*p5 = p1;
		//while (*p1 != '\0')
		//	*(p3++) = *(p1++);
		strcpy(p3, p1);
		//*p3 = '\0';
		
		while (*strReplace != '\0')
		{
			*p = *strReplace;
			p++;
			strReplace++;
		}
		while (*p3 != '\0')
		{
			*(p) = *(p3);
			++p;
			++p3;
		}
		*p = '\0';
	}
	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\n",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\n");
    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\n",s);
        printf("%ld\n",l);
        printf("%f\n",fp);
        printf("%c\n",c);
        fclose(stream);
    }
    return 0;
}




版权声明:

posted on 2015-07-12 12:12  moffis  阅读(173)  评论(0编辑  收藏  举报

导航