Tag: 字符串

数组元素是C中字符串数组的变量

我正在尝试在C中初始化一个字符串数组。我想从一个变量设置数组的一个元素,但我得到一个编译器错误。 这有什么问题? char * const APP_NAME = “test_app”; char * const array_of_strings[4] = { APP_NAME, “-f”, “/path/to/file.txt”, NULL }; 错误是error: initializer element is not constant 。

在字符串数组中排序单词

我的程序旨在允许用户输入一个字符串,我的程序将输出每个字母和单词的出现次数。 我的程序还按字母顺序排序。 我的问题是:我输出所看到的单词(第一个未排序)及其出现的表格,在我的表格中我不想重复。 解决了 例如,如果单词“to”被看到两次,我只想让“to”这个单词在我的表中只出现一次,输出出现次数。 我怎样才能解决这个问题? 另外,为什么我不能简单地将string[i] == delim为应用于每个分隔符,而不是必须为每个分隔符手动分配它? 编辑:修复了我的输出错误。 但是,我如何设置string[i]的条件等于我的代码中的任何分隔符,而不仅仅是为空格键工作? 例如,在我的输出中,如果我输入“你,你”,它将输出“你,你”,而不仅仅是“你”。 如何编写它以删除逗号并将“你,你”比作一个单词。 任何帮助表示赞赏。 我的代码如下: #include #include #include const char delim[] = “, . – !*()&^%$#@ ? []{}\\ / \””; #define SIZE 1000 void occurrences(char s[], int count[]); void lower(char s[]); int main() { char string[SIZE], words[SIZE][SIZE], temp[SIZE]; int i = 0, j = 0, […]

如何在c中写入特定的文件行?

所以我有一个项目,我正在创建一个允许用户写入文件的程序,如下所示: #include #include FILE *fd; FILE *fw; struct store { char Word[512]; char NWord[512]; } stock; struct store2 { char Definition[512]; } stock2; char done=’y’; int count=1; int c=0; int d=0; int main(void) { fw=fopen(“Test Z W.txt”,”w”); fd=fopen(“Test Z D.txt”,”w”); do { printf(“Word %d: “,count); gets(stock.Word); while((c= getchar()) != ‘\n’ && c != EOF); printf(“Definition […]

尝试将字符附加到字符数组

我正在尝试将字符“t”附加到值为“hello”的字符数组中,我正在确定数组大小,创建一个大于1个字符的新数组,分配新的字符和’\ 0’作为最后两个角色。 我不断打印旧值(你好)。 谢谢 #include #include void append(char * string,char ch) { int size; for (size=0;size<255;size++) { if (string[size]=='\0') break; } char temp[size+2]; strcpy(temp,string); temp[size+1]='t'; temp[size+2]='\0'; printf("the test string is: %s\n",temp); } int main() { char test[]="hello"; append(&test,'t'); return 0; }

如何从字符串数组C创建一个字符串

我正在学习arrays,并想知道是否有人可以帮助我。 我有一个字符串数组,需要创建一个新的字符串,它是所有数组元素的串联。 我遇到的问题是我只能打印我的数组中的第一个字符串,而不是所有字符串。 我知道我的数组中每个字符串的末尾都有一个null,那么我该如何解决这个问题呢? 也许2Darrays? 顺便说一下,我不允许使用string.h中的任何字符串操作函数。 谢谢。 #include #include int findLength(char array[]) { int i = 0; for (i = 0; array[i] != ‘\0’; i++) { } return i; } void arrayToString(char string[]) { int n = 0; int i = 0; int l = findLength(string); char *finalString; finalString = malloc(l * sizeof(char)); for (i […]

String Comparsion:Arduino C.

我正在开发基于Web的家庭自动化系统,所以我的arduino向服务器发送请求并在串行监视器中获得以下响应,以及“loneOn”,这是由于Serial.println(r); 声明。 HTTP/1.1 200 OK Date: Mon, 13 Oct 2014 17:46:03 GMT Server: Apache/2.4.4 (Win32) PHP/5.4.16 X-Powered-By: PHP/5.4.16 Content-Length: 14 Content-Type: text/html loneOn.ltwoOn. loneOn 在另一种情况下,来自服务器的响应将具有loneOff,而不是loneOn,我需要确定它究竟是哪一个。 但这不是现在的重点,我在比较字符串时遇到了麻烦。(响应会在串行监视器中循环,但同样,这不是重点。)这是arduino的代码, #include #include #include #include #include #include #include #include #include #include #include byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; byte server[] = { 192,168,137,1 } ; […]

从函数返回的字符串数组不能按预期工作

我试图将一个字符串数组传递给一个函数,在这个函数内部对它进行一些更改,然后将它传递回main()并打印它以查看更改。 它没有按预期工作。 请告诉我哪里出错了。 #include #include #include //don’t forget to declare this function char** fun(char [][20]); int main(void) { char strar[10][20] = { {“abc”}, {“def”}, {“ghi”}, {“”},{“”} }; //make sure 10 is added char** ret; //no need to allocate anything for ret, ret is just a placeholder, allocation everything done in fun int i = 0; […]

返回参数不起作用 – 给我一些奇怪的错误

这是一个应该从字符串创建子字符串的简单程序,然后它应该将子字符串作为可以打印出来的东西返回。 它实际上是一个练习,只能改变子串函数。 问题是我找不到不会引发各种警告和错误的返回类型。 我该如何更改退货类型? static void panic(const char *serror) { printf(“%s”, serror); exit(1); } static void *xmalloc(size_t size) { void *ptr; if (size == 0) panic(“Size is 0!\n”); ptr = malloc(size); if (!ptr) panic(“No mem left!\n”); return ptr; } static char *substring(const char *str, off_t pos, size_t len) { char out [len]; int index; for(index […]

在C中的#中拆分字符串

我有一个像这样的字符串: char *message = “12#34#56#78#90” 我想得到: a = “12” b = “34” c = “56” d = “78” d = “90” 谁能给我一个好方法?

指向字符串

以下两个代码片段编译时没有任何错误/警告但在运行时崩溃。 请赐教。 计划1 int main( ) { char *p= “Hello” ; *p = ‘B’ ; printf(“\n%s”,p); return 0; } 计划2 int main( ) { char *p= “Hello” ; Char *q=”mug” *q = *p ; printf(“\n%s”,q); return 0; } 对于程序2,我预计输出为’Hug’。