Tag: 字符串

为什么字符串在strcat()之后被改变了?

这是源代码 int main() { char str[]=”dance”; char str1[]=”hello”; char str2[]=”abcd”; strcat(str1,str2); printf(“%s”,str); } 输出 – bcd 为什么str在strcat(str1,str2);之后被改变strcat(str1,str2);

如何输出小于选框标志大小的选取字符串?

所以我的代码接受一串字母,然后以符号方式输出该字符串,符号大小为5.例如,如果我想输出“Hello World!”,则输出为: [Hello] [ello ] [llo W] [lo Wo] [o Wor] [ Worl] [World] [orld!] [rld! ] [ld! H] [d! He] [! Hel] [ Hell] 但问题是,如果有一串字母长度为8且字幕标志的大小为10,那么我只想在字幕标志中显示一次字符串。 因此,如果字符串的大小小于指示的选框标记,则只显示一次字符串。 例如: Input: Activist (the string that I want to output in a sign) 10 (the length of the sign) Output: [Activist ] 注意在选框标记中仍然有10个空格,它所做的只是简单地输出一次字符串。 这是我的代码。 如果有指示,我已经让它运行了不止一次: #include #include void […]

从文件缓冲一组行并将其存储在C中的数组中

这可能是一种非常低效的方法,但它的工作方式 此代码读取文件,一次在全局数组中存储8行文本(如果可能,请提供更好的选项)并调度以进行进一步处理。 这是代码 int count = 0; //global char *array_buffer[8]; //global void line(char *line_char) { int lent = strlen(line_char); array_buffer[count] = line_char; printf(“%d\n”,count); if (count == 8) { int row,col; for(row = 0; row<count; row++){ printf("%d\n",row); for(col = 0; col<=lent; col++) { printf("%c", array_buffer[row][col]); } printf("\n"); } count = 0; } count++; } int main(int […]

在C中比较两个字符串是否相同的最佳方法是什么?

我说有两个字符串(在这种情况下是字符串文字) char *name = “Fotis”; char *second_name = “Fotis”; 我有两种方法可以比较它们,它们都给我一个准确的结果。 第一个是等于运算符( == ),同样: if (name == second_name) printf (“Great success!\n”); 第二个是通过string.h提供的strcmp函数: if (strcmp (name, second_name) == 0) printf (“Great success!\n”); 我的问题是:两者中哪一个最有效率 idiomatic}比较C中两个字符串的方法? 还有另一种更惯用的方式吗?

我可以在空字符串中复制字符串吗?

假设我喜欢这样来复制字符串。 char str[] = “”; char *str2 = “abc”; strcpy(str, str2); printf(“%s”, str); // “abc” printf(“%d”, strlen(str)); // 3 然后,为什么它不会给我未定义的行为或导致程序失败。 这样做的缺点是什么?

C字符串返回函数返回垃圾

我无法从函数返回一个字符串。 它在main方法中打印出一个垃圾值。 我在这个论坛上看到了类似的问题,但该页面上的结果对我没有帮助。 我不想将另一个变量传递给函数。 我希望能够按原样返回字符串值。 我怎么能这样做? char *LookupPath(char **argv, char **dir) { /* String Name To Be Returned */ char path_name[MAX_PATH_LEN] = {0}; char *result = malloc(sizeof(path_name)); int i; /* Check To See If File Name Is Already An Absolute Path Name */ if(*argv[0] == ‘/’) { } /* Look In Path Directories */ for(i […]

C中的就地字符串反转

我正在尝试学习C的基础知识,但我无法弄清楚为什么这段代码不起作用。 reverse()中的while循环导致总线错误。 我在编程采访书中发现了几乎完全相同的代码作为有效的解决方案,但是我在这里发布的这个或其他类似的方法都没有发生总线错误。 #include void reverse(char* str) { char* end = str; char tmp = 0; if(str) { while(*end) { end++; } –end; while(end>str) { tmp = *end; *end– = *str; *str++ = tmp; } } } int main() { char* a = “12”; puts(a); reverse(a); puts(a); return 0; }

如何在C中将字符串转换为ASCII的hex代码

我有一个这样的字符串“你好” 现在我需要将它转换为每个字符的ASCII代码的hex代码,以便我可以将其插入到数据库的blob中。 任何人都可以提供任何库函数来转换字符串吗?

C:gets()跳过第一个输入

#include #include #include #include typedef struct record { char name[20]; char surname[20]; char telephone[20]; }Record; typedef struct node { Record data; struct node *next; }Node; Node *head = NULL; void addRecord(Record s) { Node *newNode; Node *n; newNode = (Node*)malloc(sizeof(Node)); newNode->data = s; newNode->next = NULL; if (head == NULL) { // The linked list […]

如何让python服务器将recvline转换为字符串?

如何将客户端的输入从服务器转换为字符串或其他一些数据类型。 我的服务器可以从客户端打印recvline,但不会将其识别为常见数据类型… void timing() { for (long i = 0; i < 200000000; i ++){ printf(""); } } int main(int argc, char **argv){ int i; int sockfd; struct sockaddr_in servaddr; char sendline[MAXLINE]; char recvline[MAXLINE]; time_t start, finish, final; start = clock(); timing(); // a function that takes a differential time based on the client finish […]