Tag: 字符串

C和C样式字符串中的Pascal样式字符串在一个函数中进行比较

我需要用C语言编写一个小函数,其中我有一个Pascal样式字符串和另一个字符串,C样式字符串。 在这个函数中,我应该比较这两个字符串,如果两个字符串相同,则函数应返回true,否则返回false。 int main() { char pascal_string // here I have a problem char c_string[] = “Home”; return 0; }

如何处理从复杂输入中提取双重类型数字(字符串)

祝大家好日子, 我对C编程很陌生,我偶然发现了一个问题,试图在几天内正确地解决,但仍然没有令人满意的结果。 我试图从这个forms的输入中提取一些double值{[ 5.1 ; 4 ], [15.3 ; -3.2] } {[ 5.1 ; 4 ], [15.3 ; -3.2] } {[ 5.1 ; 4 ], [15.3 ; -3.2] } 。 我尝试了很多方法,到目前为止最好的方法是我使用fgets()读取用户输入作为字符串,使用for循环,如果isdigit()看到一个数字我将它存储在另一个字符串中,然后我检查是否有在数字后面点,如果有,那么我把它放在数字后面的字符串,依此类推。 真正的问题是,当我想输出结果字符串并使用strtod()将其转换为我想要的double ,它只有在用户首先输入一些数字时才有效,但是如果输入看起来像{ [ 2.5 ; 1 ] } { [ 2.5 ; 1 ] }代码只是不关心,什么都不做。 任何帮助或见解将不胜感激,也许我采取了错误的方法? 谢谢。 我的代码 #include #include #include #include #include #define […]

替换C中字符串中的单词

我正在寻找一种方法来替换字符串中的单词/单词。 让我们说如果我使用这样的函数: changewords(“Hello I’m Number One”,”Number One”,”Zero”); 输出将是:“你好我是第一” – >“你好我是零” 但更多时候我也只能替换一个单词 changewords(“Hello I’m Number One”,”One”,”Four”); 像这样:“你好我是第一” – >“你好我是第四” 在我的代码中,我将句子分成单词并将每个单词与我需要更改的单词进行比较,但是我能够以这种方式只改变一个单词,任何人都可以建议如何正确地做到这一点吗? 谢谢。 这是我的代码,它使用矩阵(表)中的一行作为句子,行号是sentenceToChange变量。 int changewords (char table[][MAX_SENTENCE_LENGTH], int numOfSentences, int sentenceToChange, char subString[],char replaceWith[]){ int slen,i=0,q=0,c=0; char wordlist[100][100]; char final[1][100]; slen=strlen(table[sentenceToChange]); for(i=0;i<slen;i++) { if(table[sentenceToChange][i]!=' '){ wordlist[q][c]=table[sentenceToChange][i]; c++; } else { wordlist[q][c]='\0'; c=0; q++; } } for(i=0;i<q;i++) { […]

在C中使用strsep()和动态字符串数组

我有以下代码: #include int main(void) { char *buffer = NULL, **words = NULL, *aPtr = NULL, *sPtr; int count = 0; buffer = strdup(“The quick brown fox jumps over the lazy dog”); sPtr = buffer; do { aPtr = strsep(&sPtr, ” “); words[count++] = … // missing code } while(aPtr); return 0; } 你可以在上面看到我错过了一些代码……是否有任何类型的strdup()适用于这种情况? strdup()函数本身似乎不起作用……如果没有,我怎样才能使这段代码工作? 指针指针对我来说很头疼……

C字符串追加

我有两个我要追加的C字符串,结果应该分配给lhs变量。 我看到了一个静态初始化代码,如: char* out = “May God” “Bless You”; 。 打印输出真是”May GodBless You” 。 我理解这个结果可以输出一些未定义的行为。 代码实际上是在生产中,从来没有给出错误的结果。 并不是说我们只在一个地方发表过这样的声明。 它可以在非常稳定的代码的多个地方看到并用于形成sql查询。 C标准是否允许这种连接?

找出2个相似或不相似的字符串

规则:2个字符串,a和b,它们都由ASCII字符和非ASCII字符组成(例如,中文字符gbk编码)。 If the non-ASCII chars contained in b also show up in a and no less than the times they appear in b, then we say b is similar with a. 例如: a = “ab中ef日jkl中本” //non-ASCII chars:’中'(twice), ‘日'(once), ‘本'(once) b = “bej中中日” //non-ASCII chars:’中'(twice), ‘日'(once) c = ‘lk日日日’ //non-ASCII chars:’日'(3 times, more than twice in […]

如何替换ncurses&C中的字符串菜单项

我试图找出如何从ncurses菜单中替换item_name 。 从手册页中 ,我找不到任何set_item_name或类似的东西。 任何想法,如果有一个解决方案吗? 例如,在KEY_ENTER上替换”Choice 1″ w / “String 1” #include #include char *choices[] = { “Choice 1”, “Choice 2”, “Choice 3”, “Choice 4”, “Exit”, }; int main() { ITEM **my_items, *cur_item; int c, i; MENU *my_menu; initscr(); cbreak(); noecho(); keypad(stdscr, TRUE); my_items = (ITEM **) calloc(6, sizeof(ITEM * )); for (i = 0; […]

C – 如何在数组中存储多个字符串

想知道如何在数组中存储不同的字符串。 例如,用户将输入’qwe’,然后程序将其存储在数组变量[0]中。 输入另一个字符串然后将其存储为变量[1],依此类推 int main(int argc, char *argv[]) { char variable[1000]; int i; printf(“enter a variable\n”); scanf(“%s”, variable); for (i = 0; ??? ;i++) { printf(“The variable entered was: %s\n”,variable[i]); } return 0; 我是C的新手,所以我不知道我在做什么。 但这就是我到目前为止所提出的,并且想知道我是否可以得到一些帮助来填补其余的谢谢!

从C中的函数返回字符串数组

我从给定的文件(字典)读取单个字符串的单词,并将字符串分配给字符串数组的第n个索引。 但它不起作用。 main() for循环的输出始终为e3V\347等,而createWordTable() for循环的输出始终是字典的最后一个单词。 这是我的代码 char** createWordTable(); char** createTable(); int main() { int i; char **hashTable; hashTable = createTable(); hashTable = createWordTable(); for (i=0; i< 338; i++) { printf("%s \n",hashTable[i]); } return 0; } char** createWordTable(){ char word[20],**table; FILE *dicFile; table = createTable(); dicFile = fopen("smallDictionary.txt", "r"); if (dicFile == NULL) { perror("error"); } […]

字符串排列 – 这种回溯递归如何工作?

此函数基本上通过将字符与其所有其他字符交换来打印字符串的所有可能排列。 我理解前两次调用swap和permute。 但是为什么第二次调用掉掉了? 我无法理解这段代码。 有人可以解释一下这是如何工作的吗? /* Function to print permutations of string This function takes three parameters: 1. String 2. Starting index of the string 3. Ending index of the string. */ void permute(char *a, int l, int r) { int i; if (l == r) printf(“%s\n”, a); else { for (i = l; i […]