java中isalnum的简单介绍

博主:adminadmin 2022-11-23 14:13:09 253

今天给各位分享java中isalnum的知识,其中也会对进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

Dev c++中isalnum,islower等怎么用(给个例子)

isalnum

原型:extern int isalnum(int c);

用法:#include ctype.h

功能:判断字符变量c是否为字母或数字

说明:当c为数字0-9或字母a-z及A-Z时,返回非零值,否则返回零。

举例:

// isalnum.c

#include syslib.h

#include ctype.h

main()

{

int c;

clrscr(); // clear screen

c='a';

printf("%c:%s\n",c,isalnum(c)?"yes":"no");

c='7';

printf("%c:%s\n",c,isalnum(c)?"yes":"no");

c='@';

printf("%c:%s\n",c,isalnum(c)?"yes":"no");

getchar();

return 0;

}

islower(测试字符是否为小写字母)

相关函数 isalpha,isupper

表头文件 #includectype.h

定义函数 int islower(int c)

函数说明 检查参数c是否为小写英文字母。

返回值 若参数c为小写英文字母,则返回TRUE,否则返回NULL(0)。

附加说明 此为宏定义,非真正函数。

范例

#includectype.h

main()

{

char str[]="123@#FDsP[e?";

int i;

for(i=0;str[i]!=0;i++)

if(islower(str[i]))

printf("%c is a lower-case character\n",str[i]);

}

执行

s is a lower-case character

e is a lower-case character

请用JAVA编程输入一个语句进行词法分析

我最近正在学编译原理,我有c语言实现的词法分析程序,不知可不可以,识别的是TEST语言的单词。

#includestdio.h

#includectype.h

#includestring.h

#define keywordSum 8

char * keyword[keywordSum] = {"do", "else", "for", "if", "int", "read", "while", "write"};

char singleword[50] = "+-*(){};,:";

char doubleword[10] = "=!|";

char Scanin[300], Scanout[300];

FILE * fin, * fout;

int binaryFind(int low, int high, char * c1, char ** c2) {

int mid;

if(low high) return -1;

mid = (low+high)/2;

if(strcmp(c1, c2[mid]) == 0) return mid;

else if(strcmp(c1, c2[mid]) 0) return binaryFind(mid+1, high, c1, c2);

else return binaryFind(low, mid-1, c1, c2);

}

int TESTscan() {

char ch, token[40];

int es = 0, j, n;

printf("请输入源文件名(包括路径):");

scanf("%s", Scanin);

printf("请输入词法分析输出文件名(包括路径):");

scanf("%s", Scanout);

if((fin=fopen(Scanin, "r")) == NULL) {

printf("\n打开词法分析输入文件出错!\n");

return 1;

}

if((fout=fopen(Scanout, "w")) == NULL) {

printf("\n创建词法分析输出文件出错!\n");

return 2;

}

// printf("%c", getc(fin));

ch = getc(fin);

while(ch != EOF) {

while(ch==' ' || ch=='\n' || ch=='\t') {

ch = getc(fin);

}

if(isalpha(ch)) { //标识符

token[0] = ch;

j = 1;

ch = getc(fin);

while(isalnum(ch)) { //判断当前字符是否是字母或数字

token[j++] = ch;

ch = getc(fin);

}

token[j] = '\0';

// printf("%s", token);

n = binaryFind(0, keywordSum-1, token, keyword);

if(n 0 ) {

fprintf(fout, "%s\t%s\n", "ID", token);

} else {

fprintf(fout, "%s\t%s\n", token, token);

}

} else if(isdigit(ch)) { //数字

token[0] = ch;

j = 1;

ch = getc(fin);

while(isdigit(ch)) {

token[j++] = ch;

ch = getc(fin);

}

token[j] = '\0';

fprintf(fout, "%s\t%s\n", "NUM", token);

} else if(strchr(singleword, ch) 0) { //singleword

token[0] = ch;

token[1] = '\0';

ch = getc(fin);

fprintf(fout, "%s\t%s\n", token, token);

} else if(strchr(doubleword, ch) 0) { //doubleword

token[0] = ch;

ch = getc(fin);

if(ch=='=' (token[0]==''||token[0]=='' || token[0] == '!')) {

token[1] = ch;

token[2] = '\0';

ch = getc(fin);

} else if((ch=='')||(ch=='|')||(ch=='=') ch==token[0]) {

token[1] = ch;

token[2] = '\0';

ch = getc(fin);

} else {

token[1] = '\0';

}

fprintf(fout, "%s\t%s\n", token, token);

} else if(ch == '/') { //注释

ch = getc(fin);

if(ch == '*') {

char ch1;

ch1 = getc(fin);

do {

ch = ch1;

ch1 = getc(fin);

} while((ch!='*'||ch1!='/') ch1!=EOF);

ch = getc(fin);

} else {

token[0] = '/';

token[1] = '\0';

fprintf(fout, "%s\t%s\n", token, token);

}

} else {

token[0] = ch;

token[1] = '\0';

ch = getc(fin);

es = 3;

fprintf(fout, "%s\t%s\n", "ERROR", token);

}

}

fclose(fin);

fclose(fout);

return es;

}

void main() {

int es = 0;

es = TESTscan();

if(es 0) {

printf("词法分析有错, 编译停止!\n");

} else {

printf("词法分析成功!\n");

}

}

给你一个字典,一段文字, 编个程序,输出文字里面所有的单词个数

//字典的我就不写了,这个是前几天写的,给你参考一下

/* 实现:删除重复单词

从同目录下读入文本 wordin.txt ,进行处理,输出到 wordout.txt

*/

#includestdio.h

#includestdlib.h

#includewindows.h

int delCword(char *temp,char *newstr) //temp 为原字符串,newstr 为处理后的字符串

{ //#includestdlib.h #includewindows.h //需要头文件 存在隐患:未检测newstr的大小

typedef struct Mystr

{ char *str;

Mystr *next;

}mystr;

char last[100]="",chlast;

int n=0,k=0,flag=1;

Mystr *head=NULL,*st,*delpt,*lastpt;

newstr[0]='\0';

do

{ chlast=*temp++;

//从第一个字母开始保存于last中,遇到非字母则得到一个单词

if(isalpha(chlast) || isalnum(chlast) || '-'==chlast)

{ last[k++]=chlast; last[k]='\0'; }

else { st=head;

while(st)

{ if(!strcmp(st-str,last)) { flag=0;break; }//与现存单词比较

st=st-next;

}

if(1==flag k0) //没有重复的则存于链表中

{ Mystr *pt=(Mystr *)malloc(sizeof(Mystr));

pt-str=(char *)malloc(sizeof(char)*(k+1));

pt-next=NULL;

strcpy(pt-str,last);

if(0==n) lastpt=head=pt;

else { lastpt-next=pt; lastpt=pt; }

n++;

}

flag=1; // 重设标志位,默认为与现存字符串不重复

k=0;

}

}while(chlast);

st=head;

while(st) //复制到字符串

{ strncat(newstr,st-str,strlen(st-str)); strncat(newstr," ",1);

st=st-next;

}

st=head;

while(st) //清理垃圾

{ delpt=st-next; free(st-str); free(st);

st=delpt;

}

return n;

}

void main()

{ char *s2,*s3,ch;

FILE *fp;

int i=0;

if((fp=fopen("wordin.txt","r"))!=NULL) //从文本输入

{ fseek(fp,0,SEEK_SET);

fseek(fp,0,SEEK_END);

int longBytes=ftell(fp);// 得到文件长度

fseek(fp,0,SEEK_SET);

s3=(char *)malloc(sizeof(char)*(longBytes+1));

s2=(char *)malloc(sizeof(char)*(longBytes+1));

//fread(s3,longBytes,1,fp);

i=0;

while((ch=fgetc(fp))!=EOF)

s3[i++]=ch;

s3[i]='\0';

}

fclose(fp);

printf("\n元字符串:%s\n",s3);

printf("%d :\n%s",delCword(s3,s2),s2); //调用函数删除重复单词,并输出结果

if((fp=fopen("wordout.txt","w+"))!=NULL) //输出到文本

fwrite(s2,strlen(s2),1,fp);

fclose(fp);

printf("\n");

free(s2);

free(s3);

}

编程,输入一个正整数,输出它的所有质数因子(如180的质数因子为2、2、3、3、5)

import java.util.*;

public class Main{

public static void main(String[]args){

Scanner str=new Scanner(System.in);

long num=str.nextLong();

String result=getResult(num);

System.out.println(result);

}

public static String getResult(long num){

int pum=2;

String result="";

while(num!=1){

while(num%pum==0){

num=num/pum;

result=result+pum+"";

}

pum++;

}

return result;

}

}

扩展资料:

while循环开始后,先判断条件是否满足,如果满足就执行循环体内的语句,执行完毕后再回来判断条件是否满足,如此无限重复;直到条件不满足时,执行while循环后边的语句。简单来讲就是说while循环是先判断后循环, 判断如果满足条件进入循环 本次循环后再次判断 。

举个例子:

do-while循环与while循环的不同在于:它先执行循环中的语句,然后再判断表达式是否为真, 如果为真则继续循环;如果为假, 则终止循环。因此do-while循环至少要执行一次循环语句。 简单来讲就是说while循环是先循环后判断 。

举个例子:

总结:while循环是先判断后循环 ,而do–while循环是先循环后判断。

isalnum函数中的al是哪个单词。isalpha中的pha呢。

c语言函数

isalnum其实是is alpha number的缩写

isalpha就是is alpha的缩写

alpha这里表示所有的字母

number表示所有的数字

java中isalnum的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、java中isalnum的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-23,除非注明,否则均为首码项目网原创文章,转载请注明出处。