strcat

zoj3501

♀尐吖头ヾ 提交于 2019-11-28 02:09:06
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3501 View Code #include < iostream > #include < algorithm > #include < string .h > #include < cstdio > using namespace std; char a[][ 15 ] = { " 0 " , " I " , " II " , " III " , " IV " , " V " , " VI " , " VII " , " VIII " , " IX " }; char b[][ 15 ] = { " 0 " , " X " , " XX " , " XXX " , " XL " , " L " , " LX " , " LXX " , " LXXX " , " XC " }; char c[][ 15 ] = { " 0 " , " C " , " CC " , " CCC " , " CD " , " D " , " DC " , " DCC " , " DCCC " , " CM " }; char d[][ 15 ] = { " 0 " , " M " , " MM " , " MMM " }; struct node { char f[ 15 ];

C Primer Plus 第11章 字符串和字符串函数 11.5 字符串函数

♀尐吖头ヾ 提交于 2019-11-27 19:30:04
C库提供了许多处理字符串的函数:ANSI C 用头文件string.h给出这些函数的原型。下面是一些最有用和最常用的函数:strlen() 、strcat()、strncat() 、strcmp() 、strncmp() 、strcpy()、 strncpy()。此外,我们也将研究一下头文件stdio.h支持的sprintf()函数。 11.5.1 strlen( )函数 我们已经知道, 用strlen()函数可以得到字符串的长度。 下面的函数中用到了strlen()函数,这是一个可以缩短字符串长度的函数: /*test_fit.c-*/ void fit (char * string,unsigned int size) { if(strlen(string)>size) *(string+size)='\0'; } 这个函数确实要改变字符串,因此在函数头中声明形式参量string时没有使用const修饰符。 在程序清单11.13的程序中测试一下fit( )函数。注意,代码中用到C的字符串文本串联功能。 程序清单11.13 test.c程序 /*test.c 试用缩短字符串的函数*/ #include <stdio.h> #include <string.h> void fit(char *,unsigned int ); int main(void) { char msg[]=

C语言中字符串常用函数--strcat,strcpy

别来无恙 提交于 2019-11-27 16:32:01
strcpy 原型声明:extern char *strcpy(char* dest, const char *src); 头文件:#include < string.h > 功能:把从src地址开始且含有NULL结束符的字符串复制到以dest开始的 地址空间 说明:src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。 返回指向dest的 指针 。 函数实现: /**********************  * C语言标准库函数strcpy的一种典型的工业级的最简实现  * 返回值:目标串的地址。  * 对于出现异常的情况ANSI-C99标准并未定义,故由实现者决定返回值,通常为NULL。  * 参数:  * strDestination 目标串  * strSource 源串  ***********************/   char * strcpy ( char *strDestination, const char *strSource)  {   assert(strDestination!= NULL && strSource!= NULL);   char *strD=strDestination;   while ((*strD++=*strSource++)!= '\0');   return strDestination

How to find the length of argv[] in C

坚强是说给别人听的谎言 提交于 2019-11-27 15:57:10
问题 #include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char *argv[]){ int fir; //badly named loop variable char *input[] = calloc( strlen(argv), sizeof(char)); //initializing an array for( fir = 1; fir< strlen(argv); fir++){ //removing the first element of argv strcat(input, argv[fir]); // appending to input } } The error I'm getting is for line 7. It says "passing argument 1 of 'strlen' from incompatible pointer type". I get the same error for the strcat function. It

strcat() implementation works but causes a core dump at the end

孤人 提交于 2019-11-27 15:22:40
My implementation of strcat(char*, const char*) seems to work but then it causes a core dump. strcat() implementation: char* strcat(char* dest, const char* src) { char* tmp = dest; while(*tmp) ++tmp ; while( (*tmp++ = *src++ ) != '\0') ; return (dest); } Code in int main() where I call strcat(): char arr3[] = "Mr. "; char arr4[] = "Smith"; printf("Hello %s!", strcat(arr3, arr4)); It actually concatenated both strings and printed it out but still caused a core dump. output : Hello Mr. Smith!Aborted (core dumped) What did I do wrong? Your program doing buffer overflow at runs time, by strcat

C++中常用的字符串函数

风流意气都作罢 提交于 2019-11-27 07:42:45
C++语言提供了比C语言更丰富的字符串处理功能。它可以在字符串上经行输入,输出,合并,修改,比较,转换,复制,搜索等操作。使用这些现成的功能可以大大减少我们的编程的负担。 输入和输出的字符串函数,如printf,puts,cout,scanf,gets,cout等,在使用时应包含头文件cstdio,并使用其他字符串函数包含头文件cstring。 cstring是一个专门用于处理字符串的头文件。它包含许多字符串处理函数。由于篇幅限制,本节只能解释一些常见的内容。 字符串连接函数 strcat() strcat 就是 string catenate 的缩写,意思为把两个字符串拼在一起,其格式为: strcat(Str1, Str2); Str1、Str2 为需要拼接的字符串。 strcat() 将把 Str2 连接到 Str1 后面,并删除原来 Str1 最后的结束标志\0。这意味着,Str1 必须足够长,要能够同时容纳 Str1 和 Str2,否则字符数组会越界(超出字符串范围)。 strcat() 的返回值为 Str1 的地址。 这是一个简单的演示: #include <cstdio> #include <cstring> int main(){ char str1[100]="The URL is "; char str2[60]; cout<<"Input a URL: ";

realloc(): invalid next size when reallocating to make space for strcat on char * [duplicate]

本秂侑毒 提交于 2019-11-27 06:41:08
问题 This question already has answers here : Facing an error “*** glibc detected *** free(): invalid next size (fast)” (2 answers) Closed 5 years ago . I am getting invalid memory error on following code: printf(" %s\n","FINE 5"); printf("%s LENGTH IS: %d\n","FINE 6",strlen(": ")); buffer = (char *)realloc(buffer, strlen(buffer)* sizeof(char) + (strlen(": ")+1)* sizeof(char)); printf(" %s\n","FINE 7"); strcat(buffer, ": \0"); Output: FINE 5 FINE 6 LENGTH IS: 2 * glibc detected * ./auto: realloc()

C/C++混淆点-strcat和strcpy区别

烈酒焚心 提交于 2019-11-27 05:41:34
一、原因分析 假设: char * str=NULL; str=new char[11];你想为字符串str开辟一个存储十个字符的内存空间,然后你现在有两个字符串:char * c1="abc"和char * c2="123";你想将这两个字符串都拼接在str字符串中,你想用strcat这个函数。但是你 直接用strcat编译器会报错 !!!不能这样做, 应该在拼接之前使用memset(字符串指针,0,想要设置的个数)函数对新开辟的内存空间进行清空 ,也是用0来代替。如果不这样的话, 万一新开辟的内存空间有其它值的话,strcat就不是从第一个位置开始拼接 。对应本例的情况,应该在使用strcat之前调用memset(str,0,7);用7是有效字符个数为6,还有一个隐藏字符'\0'。 当然,还有一个方法,那就是先使用strcpy(str,c1),再使用strcat(str,c2)。为什么呢?因为 strcpy把c1拷贝至str的内存空间的时候是从 第一位 开始拷贝的 ,再用strcat拼接拷贝后的字符串就可以了。 二、msmset()函数介绍 memset函数详细说明:void *memset(void *s,int c,size_t n) 总的作用:将已开辟内存空间s的首n个字节的值设为值c。也就是将s所指向的某一块内存中的每个字节的内容全部设置为ch指定的ASCII值

error: function returns address of local variable

╄→гoц情女王★ 提交于 2019-11-26 08:54:54
I'm beginner with C and I am learning on my own. I am creating the following function: char *foo(int x){ if(x < 0){ char a[1000]; char b = "blah"; x = x - 1; char *c = foo(x); strcpy(a, b); strcat(a, c); return a; } blah ... } I am basically trying to return an appended string, but I get the following error: "error: function returns address of local variable", any suggestions, how to fix this? phoxis The local variables have a lifetime which extends only inside the block in which it is defined. The moment the control goes outside the block in which the local variable is defined, the storage

error: function returns address of local variable

佐手、 提交于 2019-11-26 02:01:22
问题 I\'m beginner with C and I am learning on my own. I am creating the following function: char *foo(int x){ if(x < 0){ char a[1000]; char b = \"blah\"; x = x - 1; char *c = foo(x); strcpy(a, b); strcat(a, c); return a; } blah ... } I am basically trying to return an appended string, but I get the following error: \"error: function returns address of local variable\", any suggestions, how to fix this? 回答1: The local variables have a lifetime which extends only inside the block in which it is