【问题标题】:How do i retrieve each string character in c我如何检索c中的每个字符串字符
【发布时间】:2021-05-04 11:28:47
【问题描述】:
for(x=0;x<Title_Size;x++){
    current_char = *(Title+x);//retrieve each character of Title array
    for(j=x;j<Title_Size;j++){
        if(current_char == *(Title+j+1)){
            //increment current_char_cnt
            current_char_cnt++;
        }//End If
    }
 }

在上面的代码中,我感兴趣的是这一行:current_char = *(Title+x); 标题是一个词,比如说:决斗从几个词的数组中得到,比如:

char *SearchTitle[6] = {"uuel","dule","speed","spede","deul","cars"};

标题最初是作为这个函数调用中的参数传递的:

HMKey = Compute_HashMapKey_for_each_title (SearchTitle[x]);

然后这就是我在函数头或原型中声明它的方式:

char Compute_HashMapKey_for_each_title (char title[])

然后在 Compute_HashMapKey_for_each_title 中调用的另一个函数被传递标题如下:

Get_Individual_Character_Frequency_For_Each_Title(title);

当然,我遇到问题的代码行是在这个特定的函数定义中:

Get_Individual_Character_Frequency_For_Each_Title

即:

void Get_Individual_Character_Frequency_For_Each_Title(char Title[]){
int Title_Size;
Title_Size = (sizeof(Title) - 4)/sizeof(Title[0]);//get size of Title
int x,j;
//Initialize current_char to char for character comparison in title_array
char current_char;
//Initialize current_char_cnt to 0
int current_char_cnt = 1;
char Alphabets[][1] = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
//Foreach character in title array, calculate individual character frequency here
for(x=0;x<Title_Size;x++){
    current_char = *(Title+x);//retrieve each character of Title array
    for(j=x;j<Title_Size;j++){
        if(current_char == *(Title+j+1)){
            //increment current_char_cnt
            current_char_cnt++;
        }//End If
    }
    /*Insert current_char_cnt in title_char_cnt_array against index - index no. represents position of char in alphabetical order*/
    if(strcmp(current_char, Alphabets[0]) == 0)
        if(title_char_cnt_array[0] == 0)//if no previous value inserted, then insert
            title_char_cnt_array[0] = current_char_cnt;
    else if(strcmp(current_char, Alphabets[1]) == 0)
        if(title_char_cnt_array[1] == 0)
            title_char_cnt_array[1] = current_char_cnt;
    else if(strcmp(current_char, Alphabets[2]) == 0)
        if(title_char_cnt_array[2] == 0)
            title_char_cnt_array[2] = current_char_cnt;
    else if(strcmp(current_char, Alphabets[3]) == 0)
        if(title_char_cnt_array[3] == 0)
            title_char_cnt_array[3] = current_char_cnt;
    else if(strcmp(current_char, Alphabets[4]) == 0)
        if(title_char_cnt_array[4] == 0)
            title_char_cnt_array[4] = current_char_cnt;
}//End For
}

现在代码一旦到达我将 current_char 与 Alphabets[0] 进行比较以查看字符是否相同的位置就会失败。

即从这里:

if(strcmp(current_char, Alphabets[0]) == 0)

即使调试器在没有任何有用信息的情况下也会关闭。我相信我在这里做错了。

使用调试器,current_char 将值 101'd' 作为它的第一个字符,例如:duel 而 Alphabets[0] 显示:“a”

我不知道问题出在哪里,但 CodeBlock 中的调试器日志显示:

在strcmp()(C:\Windows\System32\msvcrt.dll) #1 0x0000000000401688 在 Get_Individual_Character_Frequency_For_Each_Title (Title=0x405000 "uuel") 在 C:\Users\HP\Documents\MyFirstCPrj\MyFirstC\Group Similar Titles\main.c:53 C:\Users\HP\Documents\MyFirstCPrj\MyFirstC\Group Similar Titles\main.c:53:2227:beg:0x401688 在 C:\Users\HP\Documents\MyFirstCPrj\MyFirstC\Group Similar Titles\main.c:53 在strcmp() (C:\Windows\System32\msvcrt.dll) #1 0x0000000000401688 在 Get_Individual_Character_Frequency_For_Each_Title (Title=0x405000 "uuel") 在 C:\Users\HP\Documents\MyFirstCPrj\MyFirstC\Group Similar Titles\main.c:53 C:\Users\HP\Documents\MyFirstCPrj\MyFirstC\Group Similar Titles\main.c:53:2227:beg:0x401688 在 C:\Users\HP\Documents\MyFirstCPrj\MyFirstC\Group Similar Titles\main.c:53

当然这是我的头文件:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

请你告诉我问题是什么?

【问题讨论】:

  • 你在哪里看到101'd'?数组的内容是字符,current_char 必须包含单个字符。所以我怀疑你在调试器的监视窗口中看到了101'd',并假设这就是变量所包含的内容。 101 是 d 的数值,因此调试器只是向您显示两者 - 但current_char 实际上不可能同时包含两者。如果您将current_char 打印为字符,它将显示d,如果您将其打印为数字,您将看到101,因为它是用两种不同方式解释的相同数据。
  • 你使用current_char = *(Title+x);——如果你写current_char = Title[x];,它会更简单,更容易理解,而且C语言也更好。
  • Get_Individual_Character_Frequency_For_Each_Title() 显示的代码最好不要编译 - 它缺少局部变量 x、current_char、j 的声明。它也使用了太多的全局变量——假设Title_Size 和current_char_cnt 是全局变量而不是未声明的局部变量。提供不可编译的代码不会让那些可能提供帮助的人喜欢你的问题。请阅读有关如何创建 MCVE(Minimal, Complete, Verifiable Example — 或 MRE 或 SO 现在使用的任何名称)的信息。
  • @Jerry 你的观点稍微澄清了这个问题。确实,我假设 current_char 的值为 101'd' 而不是 d,因为我确实使用调试器来检查该值。话虽如此,我已经编辑了问题,以向您展示让我假设 current_char 的值是 101'd' 而不是 d 的问题。请检查并提供帮助。

标签: c for-loop pointers


【解决方案1】:

在 strcmp 中你试图将一个字符与一个字符串进行比较

//Initialize current_char to char for character comparison in title_array
char current_char;

...

if(strcmp(current_char, Alphabets[0]) == 0)

人 strcmp

int strcmp(const char *s1, const char *s2);

【讨论】:

    【解决方案2】:

    这是一个常见的 C 初学者错误:

    void Get_Individual_Character_Frequency_For_Each_Title(char Title[]){
        int Title_Size;
        Title_Size = (sizeof(Title) - 4)/sizeof(Title[0]);//get size of Title
    

    当你将一个数组传递给一个函数时,它衰减为一个指针。这意味着sizeof(Title) 是 sizeof(char *) 在函数中,这不是你想要的。如果字符串正确为空终止符,则必须使用 strlen (`Title_Size = strlen(Title);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-12
      • 1970-01-01
      • 2015-12-09
      • 2013-01-20
      相关资源
      最近更新 更多