请查看我的教程 [英] Please review my tutorial

查看:132
本文介绍了请查看我的教程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前收到了一封电子邮件,其中我被要求写一个

函数,该函数解析了最长的pallindrome的给定字符串

包含并用pallindrome替换了pallindrome另一个字符串,它的第二个参数为

。然而,在找到一个pallindrome时,所有的b $ b字符都被认为是平等的。因此,空格,逗号,

或任何标点符号都将被视为任何其他

字符。


在解决这个问题时,这是我在尝试后想出的结果或

二:


//文件Rh

#ifndef __R__

#define __R__

#include< stdio.h>

#include< stdlib.h>

#include< string.h>


#define MAX_LEN 1000

int ReplacePallindromes(char **,char *) ;

int替换(char **,const int,const int,const

char *);

char * SubString(char *, const int,const int);

void Reverse(char *);


enum MatchTypes

{

无= 0,

OddLength = 1,

EvenLength = 2

};


#endif


//文件Globals.h

#include" R.h"


int ReplacePallindromes(char ** InStr,char

* ReplaceWith)

{


int Len,LenReplaceWith,MatchFound,LastReplacement;


int Counter;

int ReverseCounter;

int SomeCounter;

int SomeOtherCounter;


char * SubStr1;

char * SubStr2;


LastReplacement = -100;

SubStr1 = SubStr2 = NULL;

MatchFound = 0;


if(* InStr == NULL || ReplaceWith == NULL)返回-1;

如果(* ReplaceWith == 0)返回-1;

LenReplaceWith = strlen(ReplaceWith);

Len = strlen(* InStr);

if(Len< 2)返回-1;


Counter = ReverseCounter = SomeCounter = SomeOtherCounte r = 0 ;


for(Counter = 0; Counter< Len; Counter ++)

{

NextCharFromFront:

for(ReverseCounter = Len-1; ReverseCounter> Counter;

ReverseCounter--)

{

if(*((* InStr) )+ Counter)== *((* InStr)+ ReverseCounter))

{

MatchFound = 1;

for(SomeCounter = 1 ,SomeOtherCounter = ReverseCounter-1;

Counter + SomeCounter< = SomeOtherCounter; SomeCounter ++,

SomeOtherCounter--)

{

SubStr1 = SubString(* InStr,Counter,

Counter + SomeCounter);


SubStr2 = SubString(* InStr,SomeOtherCounter,

ReverseCounter);

Reverse(SubStr2);


MatchFound =((strcmp(SubStr1,Sub) STR2)== 0)? 1:0);

如果(MatchFound == 0)

{

免费(SubStr1);

免费(SubStr2);

休息;

}


免费(SubStr1);

免费(SubStr2);

} // SomeCounter for循环结束

if(MatchFound == 1&& Counter + SomeCounter> = SomeOtherCounter)

{

//这是最大的pallindrome,替换它

LastReplacement =替换(InStr,Counter,

ReverseCounter,ReplaceWith);

Len = strlen(* InStr);

Counter = LastReplacement + 1;

转到NextCharFromFront;

}

} //结束InStr [Counter] == InStr [ReverseCounter]

} // ReverseCounter for循环结束

} //循环计数器结束

返回0;


} //功能结束


int Replace(char ** InStr,const int From,

const int To,const char * ReplaceWith)

{


char * NewString;

int LenReplaceWith,Coun ter,k,Len;


if(* InStr == NULL || ReplaceWith == NULL)返回-1;

Len = strlen(* InStr);

LenReplaceWith = strlen(ReplaceWith);

NewString = NULL;


NewString =(char *)malloc(sizeof(char)*(Len-(To-From + 1)+ LenReplaceWith + 1));

for(Counter = 0;

Counter<(Len-(To-From + 1)+ LenReplaceWith + 1); Counter ++)

*(NewString + Counter) = 0;


for(Counter = 0; Counter< = From-1; Counter ++)

*(NewString + Counter)= *((* InStr )+ Counter);


for(Counter = 0; Counter< LenReplaceWith; Counter ++)

*(NewString + From + Counter)= *(ReplaceWith +计数器);


for(Counter = To + 1,k = 0; Counter< Len; Counter ++,k ++)

*(NewString + From + LenReplaceWith + k)= *((* InStr)+ Coun ter);


*(NewString + From + LenReplaceWith + k)= 0;


free(* InStr);

Len = strlen(NewString);

* InStr =(char *)malloc(sizeof(char)*(Len + 1) );

if((* InStr)== NULL)返回-1;

strcpy(* InStr,NewString);

免费(NewString);


返回(From-1 + LenReplaceWith);


}

char * SubString(char * Source,const int From,const

To)

{


int Len,Counter;

char * NewString;

Len = strlen(Source);

if(Source == NULL)返回NULL;

if(From< 0)返回NULL;

if(From> To)返回NULL;

if((From> Len)|| (To> Len))返回NULL;

NewString =(char *)malloc(sizeof(char)*(To-From + 2));

for(Counter = From; Counter< = To; Counter ++)

{

*(NewString + Counter-From)= *(来源+计数器);

}

NewString [Counter-From] = 0;

返回NewString;

}


void Reverse(char * Str)

{

int i,j,len;

char temp;

i = j = len = temp = 0;


if(Str!= NULL)

{

len = strlen( Str);

for(i = 0,j = len-1; i< = j; i ++,j--)

{

temp = Str [i];

Str [i] = Str [j];

Str [j] = temp;

}

}

}

//文件RC

#include" R.h"


int main(无效)

{


char c,* InStr,* ReplaceWith;

int i;

c ='''';

i = 0;


InStr =(char *)malloc(sizeof (char)*(MAX_LEN + 1));

if(InStr == NULL)返回-1;

ReplaceWith =(char *)malloc(sizeof(char)*(MAX_LEN + 1));

if(ReplaceWith == NULL)返回-1;


printf(&\\;输入一个字符串(最多1000个字符):");

while(((c = getchar())!= EOF) &安培;&安培; (c!=''\ n'')& (i< = MAX_LEN))InStr [i ++] = c;

InStr [i] = 0;


c ='''';

i = 0;

printf(" \ n\\\
输入你要替换的字符串\ npallindromes

with(最多1000个字符) :");

while(((c = getchar())!= EOF)&&(c!=''\ n'')&(i< = MAX_LEN ))

ReplaceWith [i ++] = c;

ReplaceWith [i] = 0;

if(ReplacePallindromes(& InStr,ReplaceWith) == 0)

{

printf(\ n修改后的字符串是:\ n \ n%s \ n",InStr);

}

其他

{

printf(发生错误。程序现在将结束。) ;

免费(InStr);

免费(ReplaceWith);

返回-1;

}


免费(InStr);

免费(ReplaceWith);

返回0;

}

我被问到为什么我把一个指针指针作为Repl的第一个

参数acePallindrome功能。我以为我会写一个

的教程来解释这个我过去在练习指针的过程中通过启发式的方式得到的东西。所以这是我写的一个

教程。即使冒着被人嘲笑的风险,我也会向所有在这里学习过的人提供这个,所以你可以查看它,

因为,经过一些改进,我打算将它发布,如果它是

一切正常。我只学习C,因此只写了我自己动手编码和测试的b
$ b。请给我你的意见

它的好坏。

http://sathyaishc.tripod.com/Tutorial.doc


(您必须右键单击并说出另存为或目标另存为)


谢谢!

I had recieved an email sometime ago wherein I was asked to write a
function that parsed a given string for the longest pallindrome it
contained and replaced the pallindrome with another string, given as
its second argument. In finding a pallindrome, however, all the
characters were to be considered equal. Therefore, a space, a comma,
or any punctuation for that matter was to be treated like any other
character.

In solving that question, here''s what I came up with after a try or
two:

//File R.h
#ifndef __R__
#define __R__
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MAX_LEN 1000

int ReplacePallindromes(char**, char*);
int Replace(char**, const int, const int, const
char*);
char* SubString(char*, const int, const int);
void Reverse(char*);

enum MatchTypes
{
None=0,
OddLength=1,
EvenLength=2
};

#endif

//File Globals.h
#include "R.h"

int ReplacePallindromes(char **InStr, char
*ReplaceWith)
{

int Len, LenReplaceWith, MatchFound, LastReplacement;

int Counter;
int ReverseCounter;
int SomeCounter;
int SomeOtherCounter;

char* SubStr1;
char* SubStr2;

LastReplacement=-100;
SubStr1=SubStr2=NULL;
MatchFound=0;

if (*InStr==NULL || ReplaceWith==NULL) return -1;
if (*ReplaceWith==0) return -1;
LenReplaceWith=strlen(ReplaceWith);
Len=strlen(*InStr);
if (Len<2) return -1;

Counter=ReverseCounter=SomeCounter=SomeOtherCounte r=0;

for(Counter=0; Counter<Len; Counter++)
{
NextCharFromFront:
for(ReverseCounter=Len-1; ReverseCounter>Counter;
ReverseCounter--)
{
if (*((*InStr)+Counter) == *((*InStr)+ReverseCounter))
{
MatchFound=1;
for(SomeCounter=1, SomeOtherCounter=ReverseCounter-1;
Counter+SomeCounter<=SomeOtherCounter; SomeCounter++,
SomeOtherCounter--)
{
SubStr1=SubString(*InStr, Counter,
Counter+SomeCounter);

SubStr2=SubString(*InStr, SomeOtherCounter,
ReverseCounter);
Reverse(SubStr2);

MatchFound=((strcmp(SubStr1, SubStr2)==0)? 1:0);
if (MatchFound==0)
{
free(SubStr1);
free(SubStr2);
break;
}

free(SubStr1);
free(SubStr2);
}//End of SomeCounter for loop

if(MatchFound==1 && Counter+SomeCounter>=SomeOtherCounter)
{
//This is the largest pallindrome, replace it
LastReplacement=Replace(InStr, Counter,
ReverseCounter, ReplaceWith);
Len=strlen(*InStr);
Counter=LastReplacement+1;
goto NextCharFromFront;
}
}//End of if InStr[Counter] == InStr[ReverseCounter]
}//End of ReverseCounter for loop
}// End of Counter for loop
return 0;

} //end of function

int Replace(char **InStr, const int From,
const int To, const char *ReplaceWith)
{

char* NewString;
int LenReplaceWith, Counter, k, Len;

if(*InStr==NULL || ReplaceWith==NULL) return -1;
Len=strlen(*InStr);
LenReplaceWith=strlen(ReplaceWith);
NewString=NULL;

NewString=(char*)malloc(sizeof(char)*(Len-(To-From+1)+LenReplaceWith+1));
for(Counter=0;
Counter<(Len-(To-From+1)+LenReplaceWith+1); Counter++)
*(NewString+Counter)=0;

for(Counter=0;Counter<=From-1;Counter++)
*(NewString+Counter)=*((*InStr)+Counter);

for(Counter=0;Counter<LenReplaceWith; Counter++)
*(NewString+From+Counter)=*(ReplaceWith+Counter);

for(Counter=To+1, k=0; Counter<Len; Counter++, k++)
*(NewString+From+LenReplaceWith+k)=*((*InStr)+Coun ter);

*(NewString+From+LenReplaceWith+k)=0;

free(*InStr);
Len=strlen(NewString);
*InStr=(char*)malloc(sizeof(char)*(Len+1));
if((*InStr)==NULL) return -1;
strcpy(*InStr, NewString);
free(NewString);

return (From-1+LenReplaceWith);

}
char* SubString(char* Source, const int From, const
To)
{

int Len, Counter;
char *NewString;
Len=strlen(Source);
if(Source==NULL) return NULL;
if (From<0) return NULL;
if (From>To) return NULL;
if ((From>Len) || (To>Len)) return NULL;
NewString=(char*)malloc(sizeof(char)*(To-From+2));
for(Counter=From; Counter<=To; Counter++)
{
*(NewString+Counter-From)=*(Source+Counter);
}
NewString[Counter-From]=0;
return NewString;
}

void Reverse(char* Str)
{
int i, j, len;
char temp;
i=j=len=temp=0;

if(Str!=NULL)
{
len=strlen(Str);
for (i=0, j=len-1; i<=j; i++, j--)
{
temp=Str[i];
Str[i]=Str[j];
Str[j]=temp;
}
}
}
//File R.C
#include "R.h"

int main(void)
{

char c, *InStr, *ReplaceWith;
int i;
c='' '';
i=0;

InStr=(char*)malloc(sizeof(char)*(MAX_LEN+1));
if(InStr==NULL) return -1;
ReplaceWith=(char*)malloc(sizeof(char)*(MAX_LEN+1) );
if(ReplaceWith==NULL) return -1;

printf("\nEnter a string (1000 characters maximum):");
while(((c=getchar())!=EOF) && (c!=''\n'') & (i<=MAX_LEN)) InStr[i++]=c;
InStr[i]=0;

c='' '';
i=0;
printf("\n\nEnter the string that you wish to replace\npallindromes
with (1000 characters maximum): ");
while(((c=getchar())!=EOF) && (c!=''\n'') & (i<=MAX_LEN))
ReplaceWith[i++]=c;
ReplaceWith[i]=0;
if(ReplacePallindromes(&InStr, ReplaceWith)==0)
{
printf("\nThe modified string is:\n\n %s\n", InStr);
}
else
{
printf("An error occured. The program will now end.");
free(InStr);
free(ReplaceWith);
return -1;
}

free(InStr);
free(ReplaceWith);
return 0;
}
I was asked as to why I''d taken a pointer to a pointer as the first
argument of the ReplacePallindrome function. I thought I''d write a
tutorial to explain this thing that I''d arrived at by heuristic
endeavours in the past, when I practiced pointers. So here''s a
tutorial I''ve written. Even at the risk of being laughed at, I present
it to all of you learned people out here, so you may review it,
because, with a few refinements, I intend to get it published if it is
all ok. I am only learning C and thus have written only what I''ve
coded and tested with my own hands. Please give me your comments as to
how good or bad it is.

http://sathyaishc.tripod.com/Tutorial.doc

(You have to do a right-click and say "Save As" or "Save Target As")

Thanks!

推荐答案

" Sathyaish" <六**************** @ yahoo.com>在消息中写道

news:7b ************************** @ posting.google.c om ...
"Sathyaish" <Vi****************@yahoo.com> wrote in message
news:7b**************************@posting.google.c om...
然而,在找到一个pallindrome时,所有的
字符都被视为相等。因此,空格,逗号,
或任何标点符号都应被视为任何其他
字符。
In finding a pallindrome, however, all the
characters were to be considered equal. Therefore, a space, a comma,
or any punctuation for that matter was to be treated like any other
character.




你能否澄清一下这部分?你的意思是只考虑字母字符{a..z},

忽略大小写,会被考虑进行比较吗?最终产品是否需要原始的非字母字符?



Could you clarify this part? Do you mean that only alphabetic characters {a..z},
ignoring case, will be considered for comparison? Does the end product require
the original non-alphabetical characters?


" AngleWyrm" <无*************** @ hotmail.com>在消息中写道

news:9Wphd.340850
"AngleWyrm" <no***************@hotmail.com> wrote in message
news:9Wphd.340850


3l3.21535@attbi_s03 ...
3l3.21535@attbi_s03...
" Sathyaish" <六**************** @ yahoo.com>在消息中写道
新闻:7b ************************** @ posting.google.c om ...
"Sathyaish" <Vi****************@yahoo.com> wrote in message
news:7b**************************@posting.google.c om...
然而,在找到一个pallindrome时,所有的
字符都被认为是平等的。因此,空格,逗号,
或任何标点符号都应被视为任何其他
字符。
In finding a pallindrome, however, all the
characters were to be considered equal. Therefore, a space, a comma,
or any punctuation for that matter was to be treated like any other
character.




字符串是ab!c AB; C 123cba吗?包含pallindromeabccba,如果是,

应该返回什么字符串?



Does the string "ab!c AB;C 123cba" contain the pallindrome "abccba", and if so,
what string should be returned?


这篇关于请查看我的教程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆