gpt4 book ai didi

c - 将空格添加回我的汇编程序中

转载 作者:行者123 更新时间:2023-11-30 15:10:58 25 4
gpt4 key购买 nike

到目前为止,我得到了一段用于作业的汇编程序,该程序以类似 - main: mov %a,0x04 ; 的行开头。 sys_write。有些行包含标签(即末尾带有分号的单词),有些则不包含。 ; 之后的所有内容都是注释,需要删除。需要删除并放回空白,以便成品看起来像这样 -main: mov %a,0x04。我花了很多天的时间在这上面,想知道你们是否知道如何放入空格,因为目前它看起来像这样 - main:mov%a,0x04。任何普遍添加空格的可靠方法将不胜感激。

int i;
char line[256];
while(fgets(line,256,infile) != NULL)
{
char label[256];
int n = 0;
for( i=0; i<256; i++)
{
if(line[i] == ';') // checks for comments and removes them
{
label[n]='\0';
break;
}
else if(line[i] != ' ' && line[i] != '\n')
{
label[n] = line[i]; // label[n] contains everything except whitespaces and coms
n++;

}
}

char instruction[256];
for(n =0; n<strlen(label);n++)
{
//don't know how to look for commands like mov here
// would like to make an array that puts the spaces back in?
}

// checks if string has characters on it.
int len = strlen(label);
if(len ==0)
continue;
printf("%s\n",label);
}
fclose(infile);
return 0;

最佳答案

我将字符串分成空格之间的子字符串,然后在它们之间添加一个空格。

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

int main(){
FILE *infile=fopen("assembly","r");
char line[256];
while(fgets(line,256,infile) != NULL)
{
char* tok=strtok(line,";"); //search for comma
char* label=malloc(strlen(tok)*sizeof(char)); //allocate memory for
//string until comma

strcpy(label,""); //clean string
tok=strtok(tok," "); //search for space
while(tok != NULL){
if(strlen(label)>0) //when not empty,
strcat(label," "); //add space
strcat(label,tok);
tok=strtok(NULL," ");
}
printf("%s_\n",label);
free(label);
}
fclose(infile);
return 0;

如果你还想按照你的方式做,我会这样做

(...)
else if((line[i] != ' ' && line[i] != '\n')||(line[i-1] != ' ' && line[i] == ' '))
{ // also copy first space only
label[n] = line[i]; // label[n] contains everything except whitespaces and coms
n++;
}
}
printf("%s\n",label);
}
fclose(infile);
return 0;
}

关于c - 将空格添加回我的汇编程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35880602/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com