全站数据
8 4 2 0 5 8 1

C语言,计算字符串所占的宽度

石油工程技术 | 简单学习,快乐成才!         
问题更新日期:2024-10-15 17:10:11

问题描述

C语言,计算字符串所占的宽度急求答案,帮忙回答下
精选答案
最佳答案

#include

#include

#include

#include

#define TAB_WIDTH 8 //假设制表符的宽度为8

#define ENTER_WIDTH 0 //假设回车符的宽度为0

#define ALARM_WIDTH 0 //假设警示符的宽度为0

int cl[256];

size_t digMaxWidth(char const * str)

{

char * b;

char * e;

size_t ml;

size_t l;

if(str==NULL)

return 0;

l=strlen(str);

b=(char *)malloc(l+1);

memcpy(b,str,l+1);

e=b;

str=b;

for (;*e;e++)

{

if(*e=='

b')

{

if(b>str)

b--;

}

else

{

*b=*e;

b++;

}

}

*b=0;

for(b=str,ml=0,l=0;*b;b++)

{

if(*b=='

')

{

ml=ml>l ml:l;

l=0;

}

else

l+=cl[*(unsigned char const *)b];

}

free(str);

ml=ml>l ml:l;

return ml;

}

int main()

{

int c;

for(c=1;c<256;c++)

cl[c]=1;

cl[0]=0;

cl['

']=TAB_WIDTH;

cl['

r']=ENTER_WIDTH;

cl['

a']=ALARM_WIDTH;

printf(

"%d