String Tokenizer Using C

This was an Exercise for Me.

We were asked to perform this in Linux Environment.

Here’s the Code

#include<stdio.h>
#include<string.h>
int main()
{
char s[100],t,constant[10],alpha[10];
//char key[11][20]={“while”,”do”,”if”,”for”,”int”,”char”,”float”,”double”,”default”,”else”,”return”};
int l=0,i,q,k,j;
printf(“\nEnter any valid statement in C\n”);
scanf(“%s”,s);
printf(“\n\nToken\t\t\t\t\tCategory\n”);
l=strlen(s);
for(i=0;i<l;i++)
{
t=s[i];
if(s[i]==’#’)
{
printf(“\n%s\t\tHeader File”,s);
break;
}
else if(isalnum(t))
{
if(isdigit(t))
{
q=i;
k=0;
for(j=q;j<l;j++)
{
if(!isalnum(s[j]))
break;
constant[k]=s[j];
i=j;
k++;
}
constant[k]=”;
printf(“\n%s\t\t\t\tNumeric Constant”,constant);
}
else
{
q=i;
k=0;
for(j=q;j<l;j++)
{
if(!isalnum(s[j]))
break;
alpha[k]=s[j];
i=j;
k++;
}
alpha[k]=”;
if(strcmp(alpha,”main”)==0)
{
printf(“\n%s()\t\t\t\tMain Function”,alpha);
i++;
i++;
}
else if(strcmp(alpha,”printf”)==0 || strcmp(alpha,”scanf”)==0)
{
if(strcmp(alpha,”printf”)==0)
{
printf(“\n%s()\t\t\t\tprintf Function”,alpha);
}
else
{
printf(“\n%s()\t\t\t\tscanf Function”,alpha);
}
i++;
i++;
printf(“\n”);
while(s[i]!=’)’)
{
printf(“%c”,s[i-1]);
i++;
}
printf(“%c%c\t\t\t\tFormatted String”,s[i-1],s[i]);
//i++;
//printf(“\n While \t %c”,s[i]);
}
else if(strcmp(alpha,”while”)==0||strcmp(alpha,”do”)==0||strcmp(alpha,”if”)==0||strcmp(alpha,”for”)==0||strcmp(alpha,”int”)==0||strcmp(alpha,”char”)==0||strcmp(alpha,”float”)==0||strcmp(alpha,”double”)==0||strcmp(alpha,”default”)==0||strcmp(alpha,”else”)==0||strcmp(alpha,”return”)==0||strcmp(alpha,”case”)==0||strcmp(alpha,”switch”)==0)
{
printf(“\n%s\t\t\t\tKeyword”,alpha);
}
else
{
printf(“\n%s\t\t\t\tIdentifier”,alpha);
}
/*else
{
for(j=0;j<11;j++)
{
if(strcmp(s[j],alpha)==0)
flag=1;
}
if(flag==1)
{
printf(“\n%s——–>Keyword”,alpha);
}
else
{
printf(“\n%s——–>Identifier”,alpha);
}
flag=0;
}
else if(strcmp(alpha,”scanf”)==0)
{
printf(“\n%s()——>scanf Function”,alpha);
i++;
i++;
printf(“\n”);
while(s[i]!=’)’)
{
printf(“%c”,s[i-1]);
i++;
}
printf(“%c%c——–>Formatted String”,s[i-1],s[i]);
}*/
//printf(“\n%s——>ALPHA”,alpha);
}
}
else if(s[i]==’+’||s[i]==’-‘||s[i]==’>’||s[i]=='<‘||s[i]==’!’||s[i]==’&’||s[i]==’*’||s[i]==’/’||s[i]==’^’||s[i]==’%’||s[i]==’|’||s[i]==’=’)
{
if(t==’=’&&s[i+1]==’=’)
{
printf(“\n%c%c\t\t\t\tConditional Operators”,t,s[i+1]);
i++;
}
else if(t==’=’)
printf(“\n%c\t\t\t\tAssignment operator”,t);
else if(t==’+’&&s[i+1]==’+’)
{
printf(“\n%c%c\t\t\t\tIncrement Operator”,t,s[i+1]);
i++;
}
else if(t==’-‘&&s[i+1]==’-‘)
{
printf(“\n%c%c\t\t\t\tDecrement Operator\n”,t,s[i+1]);
i++;
}
else if(s[i+1]==’&’||(t==’!’&&s[i+1]==’=’)||s[i+1]==’|’)
{
printf(“\n%c%c\t\t\t\tLogigal Operator\n”,t,s[i+1]);
i++;
}
else
printf(“\n%c\t\t\t\tOperator”,t);
}
else
{
printf(“\n%c\t\t\t\tSpecial Character”,s[i]);
}
}
printf(“\n\n”);
return 0;
}

*****************************

Try this code. Post your comments

This code should scan out just about anything.

Please post if u find a better way to execute it too.

Since output is pretty dynamic. I did not post any output.

Tc.

#include<stdio.h>

#include<string.h>

int main()

{

char s[100],t,constant[10],alpha[10];

//char key[11][20]={“while”,”do”,”if”,”for”,”int”,”char”,”float”,”double”,”default”,”else”,”return”};

int l=0,i,q,k,j;

printf(“\nEnter any valid statement in C\n”);

scanf(“%s”,s);

printf(“\n\nToken\t\t\t\t\tCategory\n”);

l=strlen(s);

for(i=0;i<l;i++)

{

t=s[i];

if(s[i]==’#’)

{

printf(“\n%s\t\tHeader File”,s);

break;

}

else if(isalnum(t))

{

if(isdigit(t))

{

q=i;

k=0;

for(j=q;j<l;j++)

{

if(!isalnum(s[j]))

break;

constant[k]=s[j];

i=j;

k++;

}

constant[k]=”;

printf(“\n%s\t\t\t\tNumeric Constant”,constant);

}

else

{

q=i;

k=0;

for(j=q;j<l;j++)

{

if(!isalnum(s[j]))

break;

alpha[k]=s[j];

i=j;

k++;

}

alpha[k]=”;

if(strcmp(alpha,”main”)==0)

{

printf(“\n%s()\t\t\t\tMain Function”,alpha);

i++;

i++;

}

else if(strcmp(alpha,”printf”)==0 || strcmp(alpha,”scanf”)==0)

{

if(strcmp(alpha,”printf”)==0)

{

printf(“\n%s()\t\t\t\tprintf Function”,alpha);

}

else

{

printf(“\n%s()\t\t\t\tscanf Function”,alpha);

}

i++;

i++;

printf(“\n”);

while(s[i]!=’)’)

{

printf(“%c”,s[i-1]);

i++;

}

printf(“%c%c\t\t\t\tFormatted String”,s[i-1],s[i]);

//i++;

//printf(“\n While \t %c”,s[i]);

}

else if(strcmp(alpha,”while”)==0||strcmp(alpha,”do”)==0||strcmp(alpha,”if”)==0||strcmp(alpha,”for”)==0||strcmp(alpha,”int”)==0||strcmp(alpha,”char”)==0||strcmp(alpha,”float”)==0||strcmp(alpha,”double”)==0||strcmp(alpha,”default”)==0||strcmp(alpha,”else”)==0||strcmp(alpha,”return”)==0||strcmp(alpha,”case”)==0||strcmp(alpha,”switch”)==0)

{

printf(“\n%s\t\t\t\tKeyword”,alpha);

}

else

{

printf(“\n%s\t\t\t\tIdentifier”,alpha);

}

/*else

{

for(j=0;j<11;j++)

{

if(strcmp(s[j],alpha)==0)

flag=1;

}

if(flag==1)

{

printf(“\n%s——–>Keyword”,alpha);

}

else

{

printf(“\n%s——–>Identifier”,alpha);

}

flag=0;

}

else if(strcmp(alpha,”scanf”)==0)

{

printf(“\n%s()——>scanf Function”,alpha);

i++;

i++;

printf(“\n”);

while(s[i]!=’)’)

{

printf(“%c”,s[i-1]);

i++;

}

printf(“%c%c——–>Formatted String”,s[i-1],s[i]);

}*/

//printf(“\n%s——>ALPHA”,alpha);

}

}

else if(s[i]==’+’||s[i]==’-‘||s[i]==’>’||s[i]=='<‘||s[i]==’!’||s[i]==’&’||s[i]==’*’||s[i]==’/’||s[i]==’^’||s[i]==’%’||s[i]==’|’||s[i]==’=’)

{

if(t==’=’&&s[i+1]==’=’)

{

printf(“\n%c%c\t\t\t\tConditional Operators”,t,s[i+1]);

i++;

}

else if(t==’=’)

printf(“\n%c\t\t\t\tAssignment operator”,t);

else if(t==’+’&&s[i+1]==’+’)

{

printf(“\n%c%c\t\t\t\tIncrement Operator”,t,s[i+1]);

i++;

}

else if(t==’-‘&&s[i+1]==’-‘)

{

printf(“\n%c%c\t\t\t\tDecrement Operator\n”,t,s[i+1]);

i++;

}

else if(s[i+1]==’&’||(t==’!’&&s[i+1]==’=’)||s[i+1]==’|’)

{

printf(“\n%c%c\t\t\t\tLogigal Operator\n”,t,s[i+1]);

i++;

}

else

printf(“\n%c\t\t\t\tOperator”,t);

}

else

{

printf(“\n%c\t\t\t\tSpecial Character”,s[i]);

}

}

printf(“\n\n”);

return 0;

}

*

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s