从C字符串中去除第一和最后一个字符 [英] Strip first and last character from C string

查看:1404
本文介绍了从C字符串中去除第一和最后一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C字符串,它看起来像NMY stringP,其中N和P可以是任何字符。我怎么可以编辑成我的字符串用C?

I have a C string that looks like "Nmy stringP", where N and P can be any character. How can I edit it into "my string" in C?

推荐答案

要删除的第一个字符点到第二个字符:

To "remove" the 1st character point to the second character:

char mystr[] = "Nmy stringP";
char *p = mystr;
p++; /* 'N' is not in `p` */

要删除的最后一个字符替换为一个'\\ 0'

To remove the last character replace it with a '\0'

p[strlen(p)-1] = 0; /* 'P' is not in `p` (and it isn't in `mystr` either) */

这篇关于从C字符串中去除第一和最后一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆