strcat

Im getting a lineshift when trying to build a string

我们两清 提交于 2019-12-11 13:50:28
问题 Im trying to build a string to call a script with args, and one of the args is read from a file but im getting a lineshift and the output is like this /home/glennwiz/develop/c/SnuPort/ExpGetConfig.sh xogs1a 3/37 > lastConfig.txt i want the 3/37 and > lastConfig to be on the same line. this is my code. char getConfig[100] = "/home/glennwiz/develop/c/SnuPort/ExpGetConfig.sh "; char filedumpto[50] = " > lastConfig.txt"; FILE* file = fopen("rport.txt","r"); if(file == NULL) { return NULL; } fseek

Code not working as expected in C

旧时模样 提交于 2019-12-11 12:16:49
问题 I was working on a program in C to count the number of spaces in a sentence. But I haven't managed to get it to work properly. If I enter something like Hello world 1234 how are you the output I'm getting is 3 when the output expected is 5. My code is : //Program to count number of words in a given Sentence #include <stdio.h> #include <string.h> int main() { char sent[100]; char sentence[] = {' ', '\0'}; printf("\nEnter a sentence :\n"); gets(sent); strcat(sentence, sent); int l = strlen

Using realloc to concat strings

ⅰ亾dé卋堺 提交于 2019-12-11 06:17:14
问题 I'm trying to concat two strings, supposing the "dest" string hasn't enough space to add another one, so I'm using dynamic arrays to solve it. The problem is a mremap_chunk error when trying to compile the code. I don't know what I'm missing since the realloc call has all the right params place in. Error: malloc.c:2869: mremap_chunk: Assertion `((size + offset) & (GLRO (dl_pagesize) - 1)) == 0' failed. Aborted (core dumped) #include <stdio.h> #include <string.h> #include <stdlib.h> char

Confusion in “strcat function in C assumes the destination string is large enough to hold contents of source string and its own.”

风流意气都作罢 提交于 2019-12-11 04:26:32
问题 So I read that strcat function is to be used carefully as the destination string should be large enough to hold contents of its own and source string. And it was true for the following program that I wrote: #include <stdio.h> #include <string.h> int main(){ char *src, *dest; printf("Enter Source String : "); fgets(src, 10, stdin); printf("Enter destination String : "); fgets(dest, 20, stdin); strcat(dest, src); printf("Concatenated string is %s", dest); return 0; } But not true for the one

how can I strcat one character to array character in c++

纵饮孤独 提交于 2019-12-11 03:19:52
问题 I want to get one character from cin.get() and add it to a array character. I use strcat but the single character has an error. please help me if you know. thanks for all answers. void main (void) { char e[80]="hi"; char c; cin.get(c); strcat(e,c); cout << "e: " << e << endl; getch(); } This is part of my code that I want to do this. 回答1: stncat() concatenates two strings, method signature looks like this, char * strncat ( char * destination, const char * source, size_t num ); but you are

Octave strcat ignores added spaces

不想你离开。 提交于 2019-12-10 19:20:03
问题 Octave adds spaces with strcat In Octave I run these commands: strcat ("hel", " ", "lo") I get this result: ans = hello Instead of what I expected: ans = hel lo strcat to me sounds like "concatenate strings". A space is a valid character, so adding a space should be OK. Matlab has the same behaviour, so it's probably intended. I find it counter intuitive. Does this behavior makes sense? 回答1: Hmm. It works how it is defined: "strcat removes trailing white space in the arguments (except within

string类的实现:在VS2010和DEV4.9.9.2之间的差异

折月煮酒 提交于 2019-12-09 11:40:46
以下是string类的代码: string.h代码如下: #include<iostream> using namespace std; #ifndef STRING_H #define STRING_H //------------------------------------------------------------------- class String { private: char* m_pData; public: String(const char* pstr=NULL); String(const String& obj); String& operator=(const String& obj); String& operator=(const char* pstr); String& operator=(int nNum); char& operator[](int nIdx); const char& operator[](int nIdx)const; String& operator[](const String& obj); String& operator+=(const String& obj); String& operator+=(const char* pstr); int operator!=(const String&obj);

C 简陋版string操作strcpy strcmp strcat strchr strstr

空扰寡人 提交于 2019-12-09 11:39:02
文件下载地址: http://pan.baidu.com/s/1bn2BcTL 代码如下: #include <stdio.h> #include <stdlib.h> char *strcpy_(char *dest,const char *src); char *strcat_(char *dest,const char *src); int strcmp_(const char *dest,const char *src); int strlen_(const char *src); char *strchr_(char *s, int c); char *strstr_(const char *s,const char *c); int main() { char p[]="xxdexxx"; char q[]="de"; printf("p=%s\n",p); printf("q=%s\n",q); printf("strlen_(p)=%d\n",strlen_(p)); printf("strcpy_(p,q)=%s\n", strcpy_(p,q)); char p1[]="xxdexxx"; char q1[]="de"; printf("strchr_(p,'d')=%s\n",strchr_(p1,'d')); char p2[]="xxdexxx";

How to copy or concatenate two char*

狂风中的少年 提交于 2019-12-07 13:52:53
问题 How do you concatenate or copy char* together? char* totalLine; const char* line1 = "hello"; const char* line2 = "world"; strcpy(totalLine,line1); strcat(totalLine,line2); This code produces an error! segmentation fault I would guess that i would need to allocate memory to totalLine? Another question is that does the following copy memory or copy data? char* totalLine; const char* line1 = "hello"; totalLine = line1; Thanks in advance! :) 回答1: I would guess that i would need to allocate memory

Size of strcat Destination Array

笑着哭i 提交于 2019-12-07 02:27:24
问题 Take the following program: #include <iostream> #include <cstring> using namespace std; int main() { char a[8] = "Hello, "; char b[7] = "world!"; strcat(a, b); cout << a; return 0; } Notice that a and b have the same size as their assigned strings. The documentation states that for strcat(a, b) to work, a needs to be large enough to contain the concatenated resulting string. Nevertheless, cout << a displays "Hello, world!" . Am I entering undefined behavior? 回答1: "Am I entering undefined