首页 > 其他分享 >c语言替换字符串 Replace the first ‘oldstr‘ with ‘newstr‘ in ‘srcstr‘

c语言替换字符串 Replace the first ‘oldstr‘ with ‘newstr‘ in ‘srcstr‘

时间:2024-08-14 14:08:14浏览次数:4  
标签:NULL datestr oldstr int Replace char srcstr Str include

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <ctype.h>
#include <sys/stat.h>
 
void getdate(char *datestr,char *format)
{
	time_t nnowtime = time(NULL);
	struct tm* ptmTemp = localtime(&nnowtime);
	if (ptmTemp == NULL ||
		!strftime(datestr, 256, format, ptmTemp))
		datestr[0] = '\0';
}

void StrToLower(char * Str)
{
	int len;
	int i;

	if(Str == NULL) 	return;

	len = (int)strlen(Str);
	if(len <= 0)		return;

	for(i=0; i<len; i++)
	{
		if((Str[i] >= 'A') && (Str[i] <= 'Z'))
			Str[i] = 'a' + Str[i] - 'A';
	}
	return;
}

bool StrIsDigit(char * Str)
{
	char digitS[11]="0123456789";
	int  i = 0;
	
	for(i = 0; i < (int)strlen(Str); i++)
	{
		if(strchr(digitS,Str[i]) == NULL)
			return false;
	}
	return true;
}


/**********************************************************************************
*	Function:
*		1. Replace the first 'oldstr' with 'newstr' in 'srcstr'
*	Arguments:
*		IN	:
*			srcstr
*			oldstr
*			newstr
*		OUT	:
*			No
*	Return:
*		1. If find and replace 'oldstr' with 'newstr' in 'srcstr', return 1
*		2. If find no 'oldstr' in 'srcstr', return 0
*		3. If error (malloc return NULL) return -1
***********************************************************************************/
int StrReplace(char * srcstr, const char * oldstr, const char * newstr)
{
	char	*tmpbuffer;
	int		prelen, postlen, totallen, newlen, oldlen;
	char    *ptr;
	char	*tmpchar;

	tmpchar = strstr(srcstr,oldstr);
	if(tmpchar == NULL)	return 0;

	totallen=(int)strlen(srcstr);
	oldlen=(int)strlen(oldstr);
	newlen=(int)strlen(newstr);

	prelen = (int) (tmpchar - srcstr);
	postlen = totallen - prelen - oldlen;
	tmpchar += oldlen;

	tmpbuffer = (char*)malloc(prelen + newlen + postlen+1);
	if(tmpbuffer == NULL)	return -1;

	ptr = tmpbuffer;
	memcpy(ptr, srcstr, prelen);
	ptr += prelen;
	memcpy(ptr, newstr, newlen);
	ptr += newlen;
	memcpy(ptr, tmpchar, postlen);

	tmpbuffer[prelen + newlen + postlen] = 0;
	strcpy(srcstr, tmpbuffer);
	srcstr[prelen + newlen + postlen]=0;
	free((void*)tmpbuffer);
	return 1;
}

#include <iostream>
using namespace std;
int main()
{
 
    char szdate[64]={0};
    char sztime[64]={0};
    getdate(szdate, "%Y%m%d");
    getdate(sztime, "%H%M%S");
    std::cout<<szdate<<std::endl;
    std::cout<<sztime<<std::endl;
    char a = 'A';
    if(StrIsDigit(&a))
    std::cout<<"ewfa"<<std::endl;
    StrToLower(&a);
    std::cout<<a<<std::endl;
    char str123[100];
    strcpy(str123," my name my your name is hellowrold");
    StrReplace(str123,"my","your");
    std::cout<<str123<<std::endl;
    return 0;
}

 

标签:NULL,datestr,oldstr,int,Replace,char,srcstr,Str,include
From: https://www.cnblogs.com/hshy/p/18358839

相关文章

  • 高并发场景下慎用replace into来进行数据库操作
    概述REPLACEINTO操作虽然简单易用,但在使用时需要注意其带来的各种影响,包括锁粒度、性能开销、数据一致性、事务处理和并发控制等方面。在高并发和大数据量环境下,建议评估其性能影响,并根据实际需求选择合适的替代方案,如使用INSERT...ONDUPLICATEKEYUPDATE来避免不必......
  • AT_abl_e Replace Digits 题解
    题目传送门前置知识线段树解法需要维护区间信息,考虑使用线段树维护。预处理出\(\overline{xx\dotsx}\),其中\(x\in\{1,2,3,4,5,6,7,8,9\}\),便于区间赋值。然后就是普通的线段树板子了。代码#include<bits/stdc++.h>usingnamespacestd;#definelllonglong#de......
  • PHP中preg_replace函数解析
    preg_replace—执行一个正则表达式的搜索和替换mixedpreg_replace(mixed$pattern,mixed$replacement,mixed$subject)搜索subject中匹配pattern的部分,以replacement进行替换。常见于CTF竞赛中web题目中1、/g表示该表达式将用来在输入字符串中查找所有可能的匹配,返......
  • MYSQL中replace into的用法
    今天在编程的时候,学习了replaceinto的用法,真的很好用,是insertinto的增强版。在向表中插入数据时,我们经常会遇到这样的情况:1、首先判断数据是否存在;2、如果不存在,则插入;3、如果存在,则更新。###项目成本案例:::::  1IntegerupdateTransport(Reimbursementreimbursement);......
  • uniapp报错 TypeError: Cannot read properties of undefined (reading ‘replace‘)
      欢迎关注我......
  • Java 中的字符串替换方法详解:replace, replaceAll 和 replaceFirst
    在Java中,字符串的替换是一种常见的操作,特别是在处理文本和格式化输出时。Java提供了几种不同的方法来实现字符串替换,其中包括replace,replaceAll和replaceFirst。本文将详细讨论这些方法的用法、区别以及示例。1.replace(CharSequencetarget,CharSequencereplaceme......
  • CF1375D Replace by MEX 题解
    题目大意令mexmexmex为序列中最小的没有出现的数。给你一个长度为......
  • CF1707E Replace
    题目描述给定一个长为\(n\)的序列\(a_1,\ldots,a_n\),其中对于任意的\(i\)满足\(1\leqa_i\leqn\)。定义一个二元组函数如下:\[f((l,r))=(\min\{a_l,\ldots,a_r\},\max\{a_l,\ldots,a_r\})(l\leqr)\]你需要回答\(q\)次询问,每次给定\((l_i,r_i)\),问其最少经过多少......
  • Python3 笔记:字符串的 replace() 和 expandtabs()
    1、replace()方法把字符串中的old(旧字符串)替换成new(新字符串),如果指定第三个参数max,则替换不超过max次。语法:str.replace(old,new[,max])参数:old:将被替换的子字符串。new:新字符串,用于替换old子字符串。max:可选参数,如果填写则表示替换不超过max次。str1='old......
  • 【JavaScript】内置对象 - 字符串对象 ⑦ ( String 字符串替换 | replace 函数 | repl
    文章目录一、String字符串替换1、replace函数替换字符串2、使用replace函数替换所有匹配字符串3、replaceAll函数替换字符串二、String字符串转数组1、split函数切割字符串2、代码示例-切割字符串String字符串对象参考文档:https://developer.mozilla.......