当前位置:我的异常网» C语言 » C语言字符串替换。解决方法

C语言字符串替换。解决方法

www.myexceptions.net  网友分享于:2015-04-14  浏览:0次

C语言字符串替换。。。。

楼主想要把如下的字符串

"C:\Users/Frank\Documents\Visual Studio 2012\Projects\bin\regenerate_original_picture.txt"

替换成

"C:/Users/Frank/Documents/Visual Studio 2012/Projects/bin/regenerate_original_picture.txt"

然后再给fopen函数调用。。。

----------------------

自己编了一个函数,可是出现了点问题,好像是编译器识别这个"\"的符号。怎么办?

char *Str1="\" ;

char *Str2="/" ;

rep(PA,Str1,Str2);

------解决思路----------------------

#include

#include

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

char path[] = "C:\\Users\\Frank\\Documents\\Visual Studio"

" 2012\\Projects\\bin\\regenerate_original_picture.txt";

int i;

for(i = 0; i

if(path[i] == '\\')

path[i] = '/';

}

printf("%s\n", path);

return 0;

}

------解决思路----------------------

字符 '\'在C中用于转义特殊用途,使用它分割路径时,就要使用\\,才是‘\’, 就像你sprintf中拼接一样

------解决思路----------------------

C++ Character Constants

Character constants are one or more members of the “source character set,” the character set in which a program is written, surrounded by single quotation marks ('). They are used to represent characters in the “execution character set,” the character set on the machine where the program executes.

Microsoft Specific

For Microsoft C++, the source and execution character sets are both ASCII.

END Microsoft Specific

There are three kinds of character constants:

Normal character constants

Multicharacter constants

Wide-character constants

Note   Use wide-character constants in place of multicharacter constants to ensure portability.

Character constants are specified as one or more characters enclosed in single quotation marks. For example:

char ch = 'x';          // Specify normal character constant.

int mbch = 'ab';        // Specify system-dependent

//  multicharacter constant.

wchar_t wcch = L'ab';   // Specify wide-character constant.

Note that mbch is of type int. If it were declared as type char, the second byte would not be retained. A multicharacter constant has four meaningful characters; specifying more than four generates an error message.

Syntax

character-constant :

'c-char-sequence'

L'c-char-sequence'

c-char-sequence :

c-char

c-char-sequence c-char

c-char :

any member of the source character set except the single quotation mark ('), backslash (\), or newline character

escape-sequence

escape-sequence :

simple-escape-sequence

octal-escape-sequence

hexadecimal-escape-sequence

simple-escape-sequence : one of

\' \" \? \\

\a \b \f \n \r \t \v

octal-escape-sequence :

\octal-digit

\octal-digit octal-digit

\octal-digit octal-digit octal-digit

hexadecimal-escape-sequence :

\xhexadecimal-digit

hexadecimal-escape-sequence hexadecimal-digit

Microsoft C++ supports normal, multicharacter, and wide-character constants. Use wide-character constants to specify members of the extended execution character set (for example, to support an international application). Normal character constants have type char, multicharacter constants have type int, and wide-character constants have type wchar_t. (The type wchar_t is defined in the standard include files STDDEF.H, STDLIB.H, and STRING.H. The wide-character functions, however, are prototyped only in STDLIB.H.)

The only difference in specification between normal and wide-character constants is that wide-character constants are preceded by the letter L. For example:

char schar = 'x';               // Normal character constant

wchar_t wchar = L'\x81\x19';    // Wide-character constant

Table 1.2 shows reserved or nongraphic characters that are system dependent or not allowed within character constants. These characters should be represented with escape sequences.

Table 1.2   C++ Reserved or Nongraphic Characters

Character ASCII

Representation ASCII

Value Escape Sequence

Newline NL (LF) 10 or 0x0a \n

Horizontal tab HT 9 \t

Vertical tab VT 11 or 0x0b \v

Backspace BS 8 \b

Carriage return CR 13 or 0x0d \r

Formfeed FF 12 or 0x0c \f

Alert BEL 7 \a

Backslash \ 92 or 0x5c \\

Question mark ? 63 or 0x3f \?

Single quotation mark ' 39 or 0x27 \'

Double quotation mark " 34 or 0x22 \"

Octal number ooo — \ooo

Hexadecimal number hhh — \xhhh

Null character NUL 0 \0

If the character following the backslash does not specify a legal escape sequence, the result is implementation defined. In Microsoft C++, the character following the backslash is taken literally, as though the escape were not present, and a level 1 warning (“unrecognized character escape sequence”) is issued.

Octal escape sequences, specified in the form \ooo, consist of a backslash and one, two, or three octal characters. Hexadecimal escape sequences, specified in the form \xhhh, consist of the characters \x followed by a sequence of hexadecimal digits. Unlike octal escape constants, there is no limit on the number of hexadecimal digits in an escape sequence.

Octal escape sequences are terminated by the first character that is not an octal digit, or when three characters are seen. For example:

wchar_t och = L'\076a';  // Sequence terminates at a

char    ch = '\233';     // Sequence terminates after 3 characters

Similarly, hexadecimal escape sequences terminate at the first character that is not a hexadecimal digit. Because hexadecimal digits include the letters a through f (and A through F), make sure the escape sequence terminates at the intended digit.

Because the single quotation mark (') encloses character constants, use the escape sequence \' to represent enclosed single quotation marks. The double quotation mark (") can be represented without an escape sequence. The backslash character (\) is a line-continuation character when placed at the end of a line. If you want a backslash character to appear within a character constant, you must type two backslashes in a row (\\). (SeePhases of Translation in the Preprocessor Reference for more information about line continuation.)

------解决思路----------------------

/需要转义。

文章评论

c语言作业 字符串替换,C语言字符串替换。解决方法相关推荐

  1. C语言作业 第二章 C语言的数据类型

    C语言作业 第二章 C语言的数据类型 2.1 引例 运行结果: Programming is fun 说明 (1)程序中第一行: /*Output:"Programming is fun.& ...

  2. mysql json 引号 双引号_关于JSON字符串key缺少双引号的解决方法 的讲解

    JSON字符串key缺少引号的解决方法 JSON字符串是key:value形式的字符串,正常key是由双引号括起来的. 例如:<?php $data = array('name'=>'fd ...

  3. 将字符串转换为 uniqueidentifier 时失败的解决方法

    将字符串转换为 uniqueidentifier 时失败的解决方法 参考文章: (1)将字符串转换为 uniqueidentifier 时失败的解决方法 (2)https://www.cnblogs. ...

  4. c语言作业雇人写,C语言作业的链表数据的读写

    问题的引入: 这学期C语言项目我选择做一个理财管理系统,数据存储使用链式映象,也在此基础上实现数据显示功能块.在完成了数据的统计显示,按时间段显示,按类别显示等功能后,才开始考虑数据的读写的文件操作功 ...

  5. c语言作业指导1,C语言程序设计实验与习题指导

    目录 部分C语言程序设计上机实验 实验1C语言概述 实验2C语言基本数据类型 实验3C语言表达式 实验4顺序结构程序设计 实验5选择结构程序设计 实验6基本循环结构程序设计 实验7嵌套循环结构程序设计 ...

  6. 东北大学c语言作业 函数,东北大学C语言程序作业

    C语言程序设计实验报告 实验名称 循环控制语句 学 院 资源与土木工程学院 专业班级 土木工程 三班 姓 名 邢民 学 号 任课教师 焦明海 实验时间 2011年04 月25日 1.实验目的 1. 熟 ...

  7. 语言栏不见和不能输入中文的解决方法

    第一个问题是语言栏不见的问题. 第二个问题是不能输入中文,也包括中文简体-美式键盘都不见了的问题 . 首先来解决第一个问题,也就是语言栏不见的问题,主要解决方法有几种: 1,最常见的,启用了高级文字服 ...

  8. thinkphp省略php,ThinkPHP的截取字符串函数无法显示省略号的解决方法

    对于ThinkPHP的截取字符串函数无法显示省略号的情况,解决方法如下: 打开Common/extend.php页面,修改msubstr函数如下: function msubstr($str, $st ...

  9. 易语言传文本到c 崩溃,win7系统易语言打开支持库配置就崩溃的解决方法

    今天和大家分享一下win7系统易语言打开支持库配置就崩溃问题的解决方法,在使用win7系统的过程中经常不知道如何去解决win7系统易语言打开支持库配置就崩溃的问题,有什么好的办法去解决win7系统易语 ...

  10. 大学期末c语言作业演示,大学C语言期末考试练习题(带详解答案)

    资源描述: 一. 单项选择题1. ( A )是构成 C 语言程序的基本单位.A.函数 B.过程 C.子程序 D.子例程2.C 语言程序从 C 开始执行.A 程序中第一条可执行语句 B 程序中第一个函数 ...

最新文章

  1. DeepChem | Windows 10下anaconda3环境从源码构建并安装deepchem
  2. (前)首富许家印造车果然有一套:车还没量产上市,公司先冲刺科创板募资
  3. 应用程序池超出其作业限制设置_网站改版注意事项 - 蜘蛛池
  4. 【报表技术】IReport图形化报表开发工具生成PDF文档
  5. 计算机如何学会自动地进行图像美学增强?
  6. LeetCode 1234. 替换子串得到平衡字符串(滑动窗口)
  7. 论文浅尝 | Open world Knowledge Graph Completion
  8. mgy最新地址 mgyuser.com
  9. [置顶] “非主流”Web容器之TomJetty之让服务动起来
  10. Android进程与线程基本知识
  11. Java项目开发工具汇总
  12. 低版本浏览器不支持HTML5标签怎么解决?
  13. 顺丰快递查询api php,快递查询API接口_快递单号_申通顺丰数据接口 - 极速数据
  14. mysql.tar.xz怎么安装_.tar.xz文件的解压
  15. INV TXN MANAGER PUB PROCESS TRANSACTIONS
  16. 小白怎么学习云计算?2020最新云计算学习路线图
  17. 华为鸿蒙支持APP,华为 WATCH 3 已到线下店:预装鸿蒙 HarmonyOS 2,支持安装 App
  18. oracle 取系统当前年份_Oracle 之 获取当前日期及日期格式化
  19. ESP8266入门教程04:连接WIFI热点
  20. 深度学习模型的Android部署方法

热门文章

  1. 【OpenCV 4开发详解】图像上绘制几何图形
  2. 控制客户端对同一个服务端地址的连接端口数量
  3. StringBuffer的delete方法与deleteCharAt方法的区别。
  4. SQL优化常用方法36
  5. springmvc+quartz简单实现定时调度
  6. PHP:第一章——PHP中的魔术常量
  7. Node.js process 模块常用属性和方法
  8. UIbutton 圆角和边线
  9. [LeetCode]题解(python):019-Remove Nth Node From End of List
  10. 一位39岁程序员的困惑:知道得越多编程越慢怎么办?