Tag: strcat

代码在C中没有按预期工作

我正在用C编写一个程序来计算句子中的空格数。 但我还没有设法让它正常工作。 如果我输入类似Hello world 1234的内容 ,当预期输出为5时,我得到的输出是3。 我的代码是: //Program to count number of words in a given Sentence #include #include int main() { char sent[100]; char sentence[] = {‘ ‘, ‘\0’}; printf(“\nEnter a sentence :\n”); gets(sent); strcat(sentence, sent); int l = strlen(sentence), i = 0, count = 0, countCh = 0; printf(“%d”, l); char ch, ch1; […]

我的输出有一些奇怪的符号显示

我不得不为我的课程编码。 编码是关于要求用户键入他们的姓名,年龄和身份。 然后程序应该根据他们名字中的前6个字母,他们的年龄和他们学生ID中的前两个字母的密码。 问题是输出中有一个未识别的符号(╠╠╠╠╠╠╠╠╠╠╠╠╠╠)。 谁能告诉我它为什么存在>? 然后它应该计算并显示密码的长度。 这是代码: #include #include void main(void) { char name[20], id[9], age[3], passcode[10]; int x; puts(“Enter your name:\n”); gets(name); puts(“\nEnter your student id:\n”); gets(id); puts(“\nEnter your age:\n”); gets(age); x = strlen(passcode); strncpy(passcode, name, 6); strncat(passcode, id, 2); printf(“The passcode is:%s \n”,passcode); printf(“The passcode has %d characters..\n”,x); } 它看起来像: Enter your name: […]

“%s”,字符串不在字符串后打印空格

在此代码段中: printf(“shell> “); fgets(input, MAX_INPUT_SIZE, stdin); //tokenize input string, put each token into an array char *space; space = strtok(input, ” “); tokens[0] = space; int i = 1; while (space != NULL) { space = strtok(NULL, ” “); tokens[i] = space; ++i; } //copy tokens after first one into string strcpy((char*)cmdargs, (“%s “,tokens[1])); for […]

使用realloc来连接字符串

我试图连接两个字符串,假设“dest”字符串没有足够的空间来添加另一个字符串,所以我使用动态数组来解决它。 尝试编译代码时出现问题是mremap_chunk错误。 我不知道我错过了什么,因为realloc调用有所有正确的参数。 错误: malloc.c:2869: mremap_chunk: Assertion `((size + offset) & (GLRO (dl_pagesize) – 1)) == 0′ failed. Aborted (core dumped) #include #include #include char *strcatt(char *s1, char *s2) { int a = strlen(s1); int b = strlen(s2); int i, size_ab = a+b; s1 = (char *) realloc (s1, size_ab*sizeof(char)); for(i=0; i<b; i++) { s1[i+a]=s2[i]; […]

我试图建立一个字符串时得到一个换class

我试图构建一个字符串来调用带有args的脚本,其中一个args是从文件中读取但是我得到了一个换档,输出就像这样 /home/glennwiz/develop/c/SnuPort/ExpGetConfig.sh xogs1a 3/37 > lastConfig.txt 我希望3/37和> lastConfig在同一条线上。 这是我的代码。 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(file, 0, SEEK_END); long int size = ftell(file); rewind(file); char* port = calloc(size, 1); fread(port,1,size,file); strcat(getConfig, argv[1]); strcat(getConfig, port); strcat(getConfig, filedumpto); printf(getConfig); //system(getConfig); return […]

使用strcat追加字符数组不起作用

有人可以告诉我这段代码有什么问题吗??? char sms[] = “gr8″; strcat (sms, ” & :)”);

如果使用相同的数组作为两个参数,strcat()崩溃

char r[40]; strcpy(r,”abcdef”); strcat(r,r); 我的程序在第三行崩溃了吗? 替换strcat(r,r); by strcat(r,“abcdef”); 虽然工作正常….为什么?

如果strcat函数中的目标字符串不是以null结尾,那么它是否是未定义的行为?

以下程序 // Code has taken from http://ideone.com/AXClWb #include #include #define SIZE1 5 #define SIZE2 10 #define SIZE3 15 int main(void){ char a[SIZE1] = “Hello”; char b[SIZE2] = ” World”; char res[SIZE3] = {0}; for (int i=0 ; i<SIZE1 ; i++){ res[i] = a[i]; } strcat(res, b); printf("The new string is: %s\n",res); return 0; } 有明确的行为。 […]

写出比malloced更多的字符。 为什么不失败?

为什么以下工作并没有抛出某种分段错误? char *path = “/usr/bin/”; char *random = “012”; // path + random + \0 // so its malloc(13), but I get 16 bytes due to memory alignment (im on 32bit) newPath = (char *) malloc(strlen(path) + strlen(random) + 1); strcat(newPath, path); strcat(newPath, “random”); // newPath is now: “/usr/bin/012\0” which makes 13 characters. 但是,如果我添加 strcat(newPath, […]

如何在C中找到argv 的长度

#include #include #include 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 } } 我得到的错误是第7行。它说“从不兼容的指针类型传递’strlen’的参数1”。 我得到了strcat函数的相同错误。 它还说“给了一个char **但是期望一个const char * ”这两个函数。 我正在尝试使用argv所有元素填充一个新数组,除了第一个。 我试过argv = &argv[1]但它没有用。 […]