C Programs

[1] Program that removes the string "the" from the given input.

Using array string is read. Consecutive three characters are copied to another array and checks whether its a " the " (with space in front and back). If "the" is found, next characters are copied back three places. This is continued untill no characters are left( until string "length-2").

Note: gets() is used to take string since it takes space also. scanf() when used with %s terminates on white-space.

#include<stdio.h>

#include<string.h>

main()

{

char str[100],substr[4];

int i,j,k,l,m;

printf("Enter the string:");

gets(str);

for(i=0;i<(strlen(str)-2);i++)

{

k=i;

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

{

substr[j]=str[k];

k++;

}

substr[3]='\0';

if((strcmp(substr,"the")==0 && str[i-1]==' ' && str[i+3]==' ') || (strcmp(substr,"the")==0 && str[i-1]==' ' && str[i+3]=='\0') || (strcmp(substr,"the")==0 && i<1 && str[i+3]==' '))

{

printf("The detected\n");

m=i;

while(str[m+4]!='\0')

{

str[m]=str[m+4];

m++;

}

str[m]='\0';

}

}

printf("The new string is \"%s\"\n",str);

}

jestinjoy@debian:~$ gcc the.c

/tmp/ccrvWsXq.o: In function `main':

the.c:(.text+0x28): warning: the `gets' function is dangerous and should not be used.

jestinjoy@debian:~$ ./a.out

Enter the string:this is the most impotant thing in the world

The detected

The detected

The new string is "this is most impotant thing in world"

[2] atoi implementation in C

#include<stdio.h>

#include<stdlib.h>

main()

{

char *c;

int *i;

i=(int *)malloc(2);

c=(char *)malloc(100);

printf("Enter the string ");

gets(c);

while(*c <= '9' && *c >= '0')

{

*i= (*i)*10+(*c-'0');

c++;

}

printf("%d\n",*i);

}

jestinjoy@debian:~$ ./a.out

Enter the string 123.44asd

123