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

BPSK Simulation in MATLAB

Actually flicked the program from somewhere in a forum.

Took some time to understand it first. Few months back.

Now i’ve coded it better with some tweaks. So that you understand it sooner than me. 🙂

 

Here’s the code, I’ve copied and changed it to Italics, copy, paste, save and run.
Post your doubts in the comments 

clear all;
close all;

f = 2; %frequency of sine wave
fs = 100; %sampling period of the sine wave
t = 0:1/fs:1; %splitting time into segments of 1/fs
%setting the phase shifts for the different BPSK signals
p1 = 0;
p2 = pi;
%getting the number bits to be modulated
N = input(‘enter the number of bits to be modulated: N = ‘);
%generating the random signal
bit_stream=round(rand(1,N));
%allocating the dynamic variables
time = [];
digital_signal = [];
PSK = [];
carrier_signal = [];

%GENERATING THE SIGNALS
for ii = 1:1:N
%the original digital signal is
if bit_stream(ii) == 0
bit = zeros(1,length(t));
else
bit = ones(1,length(t));
end
% bit0 = (bit_stream(ii)==0)*zeros(1,length(t));
% bit1 = (bit_stream(ii)==1)*ones(1,length(t));
digital_signal = [digital_signal bit];

%Generating the BPSK signal
if bit_stream(ii) == 0
bit = sin(2*pi*f*t+p1);
else
bit = sin(2*pi*f*t+p2);
end
PSK = [PSK bit];

%Generating the carrier wave
carrier = sin(2*f*t*pi);
carrier_signal = [carrier_signal carrier];

time = [time t];
t = t + 1;

end

subplot(3,1,1);
plot(time,digital_signal,’r’);
grid on;
axis([0 time(end) -0.5 1.5]);

subplot(3,1,2);
plot(time,PSK);
grid on;
% axis([0 time(end) -2 2]);
axis tight;

subplot(3,1,3);
plot(time,carrier_signal);
grid on;
axis tight;

 

 

The output should look something like this … 
Image

 

First Post

About the blog

Well i do study a lot of stuff everyday.

Why not share my progress with the world 

They can learn along with me. If they wish that is.

I can post the Topics that i learnt. experiments performed on matlab and all that.

I can even post links from different websites that clearly explains particular concepts or copy and paste them on my blog so that it doesnt go anywhere. It would help me, and serve as a reference for the future and also help others.

Hope one day it turns out to be a good reference. That day i would purchase this link.

Hello world!

Welcome to WordPress.com. After you read this, you should delete and write your own post, with a new title above. Or hit Add New on the left (of the admin dashboard) to start a fresh post.

Here are some suggestions for your first post.

  1. You can find new ideas for what to blog about by reading the Daily Post.
  2. Add PressThis to your browser. It creates a new blog post for you about any interesting  page you read on the web.
  3. Make some changes to this page, and then hit preview on the right. You can always preview any post or edit it before you share it to the world.