#include
#include
#include
#define MAX_LINE_LEN 256
#define NULL_TERM '\0'
/* A function that will print the content of the token_list */
int printcharlist(char **tok_list)
{
char **pptr = tok_list;
while (*pptr) {
printf ("++ %s\n", *pptr);
pptr++;
}
return 0;
}
/* returns the string between 2 pointers (not forgetting to append NULL_TERM */
char *getStr(char *start, char *end)
{
int length=end-start;
int i=0;
char *result = (char *)malloc (sizeof (char) * (length+1));
while (i < length) {
result[i] = *start;
start++;
i++;
}
result[i] = NULL_TERM;
return result;
}
Proggramming Tutorials & Web Development Technologies - Tutorials for PHP,Laravel,Codeigniter,Yii,Bootstrap,Ajax,JQuery
Tuesday, 19 August 2014
Splitting a string into tokens in C
Finding Tokens in a String Using Strock
#include
#include
#include
void main()
{
char str[80];
char s[]={' ',',',':',';'};
char *t;
clrscr();
printf("enter value\n");
gets(str);
/* get the first token */
t = strtok(str, s);
printf("token is ::\n");
/* walk through other tokens */
while( t != NULL )
{
printf( " %s\n", t );
t = strtok(NULL, s);
;
}
#include
#include
void main()
{
char str[80];
char s[]={' ',',',':',';'};
char *t;
clrscr();
printf("enter value\n");
gets(str);
/* get the first token */
t = strtok(str, s);
printf("token is ::\n");
/* walk through other tokens */
while( t != NULL )
{
printf( " %s\n", t );
t = strtok(NULL, s);
;
}
Finding Tokens in a String Using C Language
#include
#include
#include
void main()
{
char a[80];
int i=0;
clrscr();
printf("Enter the string\n");
gets(a);
while(a[i]!='\0')
{
for(;a[i]==' ';i++);
{
for(;a[i]!=' '&&a[i]!='\0';i++)
printf("%c",a[i]);
}
printf("%c \n",a[i]);}
getch();
}
Remove Space In String Using C
#include
#include
#include
void main()
{
char a[80];
int i=0;
clrscr();
printf("Enter the string\n");
gets(a);
while(a[i]!='\0')
{
for(;a[i]==' ';i++);
{
for(;a[i]!=' '&&a[i]!='\0';i++)
printf("%c",a[i]);
}
printf("%c \n",a[i]);}
getch();
}
Accept 10 numbers and store in an array, print sorted array in descending and ascending order
#include<stdio.h>
#include<conio.h>
for(i=0;i<10 i="" nbsp="" span="">
scanf("%d",&a[i]);
for(i=0;i<10 i="" span="">
{
for(j=i+1;j<10 j="" span="">
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(i=0;i<10 i="" span="">
printf(" %d ",a[i]);
printf(" Nos sorted in ascending order\n");
printf("\n");
for(i=9;i>=0;i--)
printf(" %d ",a[i]);
printf(" Nos sorted in descending order\n");
getch();
}10>10>10>10>
Sunday, 6 July 2014
Postfix Calculations In C
#include<stdio.h>
#include<conio.h>
#define STACKSIZE 20
typedef struct{
int top;
int items[STACKSIZE]; /*stack can contain up to 20 integers*/
}STACK;
void push(STACK *, int);
int pop(STACK *);
int calculate(char []);
int main()
{
int result;
char E[50];
printf("Enter your Postfix expression(don't use space character):\n");
scanf("%s",E);
result = calculate(E);
printf("The result of the Postfix expression %s=%d\n",E,result);
return 0;
}
int calculate(char exp[])
{
STACK s;
s.top =-1;/*indicates that the stack is empty at the beginning*/
int i,num1,num2,value;
for(i=0; exp[i]!='\0';i++){
if(exp[i] >='0' && exp[i] <='9') /*checks if exp[i] has a digit*/
push(&s,(int)(exp[i] -'0')); /*converts digit into integer*/
else{
num1=pop(&s);
num2=pop(&s);
switch(exp[i]){
case '+': value=num2+num1;break;
case '-': value=num2-num1;break;
case '*': value=num2*num1;break;
case '/': value=num2/num1;break;
default : printf("Illegal Operator\n");
exit(1);
}
push(&s,value);
}
}
return pop(&s);
}
void push(STACK *Sptr, int ps) /*pushes ps into stack*/
{
if(Sptr->top == STACKSIZE-1){
printf("Stack is full\n");
exit(1); /*exit from the function*/
}
else {
Sptr->top++;
Sptr->items[Sptr->top]= ps;
}
}
int pop(STACK *Sptr)
{
int pp;
if(Sptr->top == -1){
printf("Stack is empty\n");
exit(1); /*exit from the function*/
}
else {
pp = Sptr->items[Sptr->top];
Sptr->top--;
}
return pp;
}
Find Postfix Calculations In C
#include<stdio.h>
#include<conio.h>
#define STACKSIZE 20
typedef struct{
int top;
int items[STACKSIZE]; /*stack can contain up to 20 integers*/
}STACK;
void push(STACK *, int);
int pop(STACK *);
int calculate(char []);
int main()
{
int result;
char E[50];
printf("Enter your Postfix expression(don't use space character):\n");
scanf("%s",E);
result = calculate(E);
printf("The result of the Postfix expression %s=%d\n",E,result);
return 0;
}
int calculate(char exp[])
{
STACK s;
s.top =-1;/*indicates that the stack is empty at the beginning*/
int i,num1,num2,value;
for(i=0; exp[i]!='\0';i++){
if(exp[i] >='0' && exp[i] <='9') /*checks if exp[i] has a digit*/
push(&s,(int)(exp[i] -'0')); /*converts digit into integer*/
else{
num1=pop(&s);
num2=pop(&s);
switch(exp[i]){
case '+': value=num2+num1;break;
case '-': value=num2-num1;break;
case '*': value=num2*num1;break;
case '/': value=num2/num1;break;
default : printf("Illegal Operator\n");
exit(1);
}
push(&s,value);
}
}
return pop(&s);
}
void push(STACK *Sptr, int ps) /*pushes ps into stack*/
{
if(Sptr->top == STACKSIZE-1){
printf("Stack is full\n");
exit(1); /*exit from the function*/
}
else {
Sptr->top++;
Sptr->items[Sptr->top]= ps;
}
}
int pop(STACK *Sptr)
{
int pp;
if(Sptr->top == -1){
printf("Stack is empty\n");
exit(1); /*exit from the function*/
}
else {
pp = Sptr->items[Sptr->top];
Sptr->top--;
}
return pp;
}
Subscribe to:
Posts (Atom)