字符串截取函数substr()的问题

scygg 2003-06-04 02:44:46
我再VC++6下写C代码,在用到字符串截取函数substr(char *s1,int pos int len)是编译调试通不过,查string.h文件,内无substr()函数定义,请问高手:怎样解决??定送分!!!!!
...全文
1743 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
norikos 2003-06-07
  • 打赏
  • 举报
回复
_Myt substr(size_type _P = 0, size_type _M = npos) const
{return (_Myt(*this, _P, _M)); }
scygg 2003-06-07
  • 打赏
  • 举报
回复
Cybergate() :我的字符串反转程序是对的!我已经调试运行通过,您试过了吗?但是字符串截取就有问题了,我的程序都是在C下编写、编译运行的,而不是C++.
Cybergate 2003-06-07
  • 打赏
  • 举报
回复
至于为什么不可以返回局部变量的地址, 也可以请热心的steedhorse(晨星)老兄解释一下:)
Cybergate 2003-06-07
  • 打赏
  • 举报
回复
你的那个程序是错误的, 不可以能返回局部变量的地址, 具体参看effective c++

字符串反转:
void reverse(char *p, int n)
{
char *q, c;
for (q=p+n-1; p<q; p++, q--) {
c=*q; *q=*p; *p=c;
}
}
scygg 2003-06-06
  • 打赏
  • 举报
回复
有个问题:我要写字符串截取函数入下,怎么有六个警告
char *substr(char *s, int pos, int len)
{
char sl[50];
int i,j,endpos;
pos--;
endpos=pos+len-1;
for(i=pos,j=0;i<=endpos;i++,j++)
{
sl[j]=s[i];
}
sl[len]='\0';
return sl;
}
警告如下所示:
substring11.c
C:\My Documents\example\22\substring11.c(5) : warning C4028: formal parameter 1 different from declaration
C:\My Documents\example\22\substring11.c(5) : warning C4028: formal parameter 2 different from declaration
C:\My Documents\example\22\substring11.c(5) : warning C4028: formal parameter 3 different from declaration
C:\My Documents\example\22\substring11.c(15) : warning C4172: returning address of local variable or temporary
C:\My Documents\example\22\substring11.c(31) : warning C4047: 'function' : 'const char *' differs in levels of indirection from 'const int '
C:\My Documents\example\22\substring11.c(31) : warning C4024: 'substr' : different types for formal and actual parameter 2

substring11.obj - 0 error(s), 6 warning(s)
请教为何?编译.exe文件通过,但结果不对啊!请高手帮忙!!
晨星 2003-06-06
  • 打赏
  • 举报
回复
呵呵,男生女生无所谓,这里又不是那种网站,^_^
steedhorse@163.com
scygg 2003-06-06
  • 打赏
  • 举报
回复
请问steedhorse(晨星):您的Email是多少啊?怎么和您联系,想和您教朋友!!我是男生!
scygg 2003-06-06
  • 打赏
  • 举报
回复
呵呵!我也写出来了
char *reverse(char *s1)
{
char *s;
int i=0,j=0,len1;
for(len1=0;s1[len1];len1++);
s=(char *)malloc(sizeof(len1));
if (len1%2!=0) return 0;
while(i<(len1/2))
{
for(j=0;j<2;j++)
{
s[i+j]=s1[len1-i-(2-j)];
}
i+=2;
}
s[len1]='\0';
return s;
}
还有个问题:我要写字符串截取函数入下,怎么有六个警告
char *substr(char *s, int pos, int len)
{
char sl[50];
int i,j,endpos;
pos--;
endpos=pos+len-1;
for(i=pos,j=0;i<=endpos;i++,j++)
{
sl[j]=s[i];
}
sl[len]='\0';
return sl;
}
警告如下所示:
substring11.c
C:\My Documents\example\22\substring11.c(5) : warning C4028: formal parameter 1 different from declaration
C:\My Documents\example\22\substring11.c(5) : warning C4028: formal parameter 2 different from declaration
C:\My Documents\example\22\substring11.c(5) : warning C4028: formal parameter 3 different from declaration
C:\My Documents\example\22\substring11.c(15) : warning C4172: returning address of local variable or temporary
C:\My Documents\example\22\substring11.c(31) : warning C4047: 'function' : 'const char *' differs in levels of indirection from 'const int '
C:\My Documents\example\22\substring11.c(31) : warning C4024: 'substr' : different types for formal and actual parameter 2

substring11.obj - 0 error(s), 6 warning(s)
请教为何?编译.exe文件通过,但结果不对啊!
晨星 2003-06-05
  • 打赏
  • 举报
回复
呵呵,最简单的函数可以这样写(调用前需确保字符串长度是偶数):

void revert(char* s)
{
short *p = (short*)s;
int len = strlen(s) / 2;
for(int i = 0 ; i < len / 2 ; i++)
{
short tmp = p[i];
p[i] = p[len - i - 1];
p[len - i - 1] = tmp;
}
}

^_^
scygg 2003-06-05
  • 打赏
  • 举报
回复
请问:我要用C编写程序将字符串"A3552E5D"按每2位倒转为"5D2E55A3"如何实现?请大侠帮忙,定送分,分不够再加。
晨星 2003-06-04
  • 打赏
  • 举报
回复
同意楼上,strncpy函数语义是:从第二个参数所指向的字符串中拷贝字符到第一个,最多拷贝的字符数由第三个决定,如果不足,则补上一堆'\0'。
Cybergate 2003-06-04
  • 打赏
  • 举报
回复
把字符串SRC从第pos位(0为开始下标,实际上是第三个字符)开始取len个字符保存到DEST中

strncpy(DEST, SRC+pos, len);
scygg 2003-06-04
  • 打赏
  • 举报
回复
请问steedhorse(晨星):在C下不能用了吗?我要在C下用啊!!!
晨星 2003-06-04
  • 打赏
  • 举报
回复
编译器会按照扩展名决定按照什么文件编译,如果按C编译,则任何C++的语法都是通不过的。
还有,以后不妨使用一下论坛的短消息功能,那样我就能及时看到了。^_^。
scygg 2003-06-04
  • 打赏
  • 举报
回复
请问steedhorse(晨星):我要写成C文件呢?写成C++是可以的,但写成(.C)文件就是不行,请教是何故?在线等待!!
scygg 2003-06-04
  • 打赏
  • 举报
回复
请问steedhorse(晨星):
晨星 2003-06-04
  • 打赏
  • 举报
回复
#include <string>
#include <iostream>
using namespace std;

void main()
{
string s = "abcdefg";
string s1 = s.substr(2 , 2);

cout << s << endl << s1 << endl;
}
pzytony 2003-06-04
  • 打赏
  • 举报
回复

basic_string::substr
basic_string substr(size_type pos = 0,
size_type n = npos) const;
The member function returns an object whose controlled sequence is a copy of up to n elements of the controlled sequence beginning at position pos.

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧