C Programs Contributed By SHARJEEL BILALI
If any body have any Suggestions or Problems then You can contact bilali.sharjeel@gmail.com
////////////////****************************************CODES***********************///////////////////
///// Calculator Application
#include <stdio.h> /* Standard input/output */
#include <string.h> /* For string functions */
#include <ctype.h> /* For classifying characters */
#include <stdlib.h> /* For converting strings to numeric values */
#include <math.h> /* For power() function */
void main()
{
char input[256]; /* Input expression */
char number_string[30]; /* Stores a number string from input */
char op = 0; /* Stores an operator */
unsigned int index = 0; /* Index of the current a character in input */
unsigned int to = 0; /* To index for copying input to itself */
size_t input_length = 0; /* Length of the string in input */
unsigned int number_length = 0; /* Length of the string in number_string */
double result = 0.0; /* The result of an operation */
double number = 0.0; /* Stores the value of number_string */
printf("\nTo use this calculator, enter any expression with"
" or without spaces");
printf("\nAn expression may include the operators:");
printf("\n +, -, *, /, %%, or ^(raise to a power).");
printf("\nUse = at the beginning of a line to operate on ");
printf("\nthe result of the previous calculation.");
printf("\nUse quit by itself to stop the calculator.\n\n");
/* The main calculator loop */
while(strcmp(gets(input), "quit") != 0)
{
input_length = strlen(input); /* Get the input string length */
/* Remove all spaces from the input by copy the string to itself */
/* including the string terminating character */
for(to = 0, index = 0 ; index<=input_length ; index++)
if(*(input+index) != ' ') /* If it is not a space */
*(input+to++) = *(input+index); /* Copy the character */
input_length = strlen(input); /* Get the new string length */
index = 0; /* Start at the first character */
if(input[index]== '=') /* Is there =? */
index++; /* Yes so skip over it */
else
{ /* No - look for the left operand */
/* Look for a number that is the left operand for the 1st operator */
/* Check for sign and copy it */
number_length = 0; /* Initialize length */
if(input[index]=='+' || input[index]=='-') /* Is it + or -? */
*(number_string+number_length++) = *(input+index++); /* Yes so copy it */
/* Copy all following digits */
for( ; isdigit(*(input+index)) ; index++) /* Is it a digit? */
*(number_string+number_length++) = *(input+index); /* Yes - Copy it */
/* copy any fractional part */
if(*(input+index)=='.') /* Is it decimal point? */
{ /* Yes so copy the decimal point and the following digits */
*(number_string+number_length++) = *(input+index++); /* Copy point */
for( ; isdigit(*(input+index)) ; index++) /* For each digit */
*(number_string+number_length++) = *(input+index); /* copy it */
}
*(number_string+number_length) = '\0'; /* Append string terminator */
/* If we have a left operand, the length of number_string */
/* will be > 0. In this case convert to a double so we */
/* can use it in the calculation */
if(number_length>0)
result = atof(number_string); /* Store first number as result */
}
/* Now look for 'op number' combinations */
for(;index < input_length;)
{
op = *(input+index++); /* Get the operator */
/* Copy the next operand and store it in number */
number_length = 0; /* Initialize the length */
/* Check for sign and copy it */
if(input[index]=='+' || input[index]=='-') /* Is it + or -? */
*(number_string+number_length++) = *(input+index++); /* Yes - copy it. */
/* Copy all following digits */
for( ; isdigit(*(input+index)) ; index++) /* For each digit */
*(number_string+number_length++) = *(input+index); /* copy it. */
/* copy any fractional part */
if(*(input+index)=='.') /* Is is a decimal point? */
{ /* Copy the decimal point and the following digits */
/* Copy point */
*(number_string+number_length++) = *(input+index++);
for( ; isdigit(*(input+index)) ; index++) /* For each digit */
*(number_string+number_length++) = *(input+index); /* copy it. */
}
*(number_string+number_length) = '\0'; /* terminate string */
/* Convert to a double so we can use it in the calculation */
number = atof(number_string);
/* Execute operation, as 'result op= number' */
switch(op)
{
case '+': /* Addition */
result += number;
break;
case '-': /* Subtraction */
result -= number;
break;
case '*': /* Multiplication */
result *= number;
break;
case '/': /* Division */
/* Check second operand for zero */
if(number == 0)
printf("\n\n\aDivision by zero error!\n");
else
result /= number;
break;
case '%': /* Modulus operator - remainder */
/* Check second operand for zero */
if((long)number == 0)
printf("\n\n\aDivision by zero error!\n");
else
result = (double)((long)result % (long)number);
break;
case '^': /* Raise to a power */
result = pow(result, number);
break;
default: /* Invalid operation or bad input */
printf("\n\n\aIllegal operation!\n");
}
}
printf("= %f\n", result); /* Output the result */
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
SYSTEM_TIME_AND_DATE
CODE:1-
#include <time.h>
#include <stdio.h>
void main( )
{
/*char dbuffer [9];
char tbuffer [9];
_strdate( dbuffer );
printf( "The current date is %s \n", dbuffer );
_strtime( tbuffer );
printf( "The current time is %s \n", tbuffer );*/
CODE:2-
#include<stdio.h>
#include<conio.h>
#include <Windows.h>
#include <stdlib.h>
#include<time.h>
void main()
{
char date[100];
char s2[100];
char ax[100];
int i,j;
SYSTEMTIME st;
GetSystemTime(&st);
printf("Year:%d\nMonth:%d\nDate:%d\nHour:%d\nMin:%d\nSecond:% d\n" ,st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond);
}
CODE:3-
#include <stdio.h>
#include <time.h>
int main ()
{
char *date;
time_t timer;
timer=time(NULL);
date = asctime(localtime(&timer));
printf("Current Date: %s", date);
getchar();
return 0;
}
(POSITIONING CURSER ON SPECIFIC LOCATION IN VC++ WITHAOUT USING GOTO() )
CODE:1-
#include<cstdio>
#include<stdio.h>
#include<conio.h>
VOID MAIN()
{
HANDLE hConsole = GetStdHandle ( STD_OUTPUT_HANDLE );
if (INVALID_HANDLE_VALUE != hConsole)
{
COORD pos = {15, 10};
SetConsoleCursorPosition( hConsole, pos );
printf("\n SHARJEEL BILALI");
}
}
(READING INI FILE USING GETPRIVATEPROFILE())
"INI FILE IS GIVIN BLOW:FIRST SEE THIS FILE AND THEN SEE THE CODE BELOW"
[db]
server=localhost
user=root
password=microsoft
database=mysql
/*******************Here in this ini file the value written under [] is called section i.e [db]
& the keys are server,user,pasword,database and the value of those corresponding keys are: "localhost","root","microsoft","mysql".KEY NOT FOUND is a default string if key is not found in section***************************//////
CODE:1-
#include<windows.h>
void main()
{
char s[20];
char u[20];
char p[20];
char d[20];
GetPrivateProfileString("db","server","KEY NOT FOUND",s,20,"C:\\myfirst.ini");
GetPrivateProfileString("db","user","KEY NOT FOUND",u,20,"C:\\myfirst.ini");
GetPrivateProfileString("db","password","KEY NOT FOUND",p,20,"C:\\myfirst.ini");
GetPrivateProfileString("db","database","KEY NOT FOUND",d,20,"C:\\myfirst.ini");
printf("\n%s\n",s);
printf("\n%s\n",u);
printf("\n%s\n",p);
printf("\n%s\n",d);
}
(SORTINGS)
CODE:1-BINARY SEARCH
#include <stdio.h>
#include <stdlib.h>
int values[] = { 1 , 2 , 3, 4 , 9 , 10 };
int compare (const void * a, const void * b) {
return ( *(int*)a - *(int*)b );
}
int main ()
{
int *pos;
int key = 9;
pos = (int*) bsearch (&key, values, 6, sizeof (int), compare);
if ( pos != NULL )
printf ("%d is in the array", *pos);
else
printf ("%d is not in the array", key);
return 0;
}
CODE:2-BUBBLE SORT
#include <stdio.h>
#define MAX 10
void swap(int *x,int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
void bsort(int list[])
{
int i,j;
for(i=0;i<(MAX-1);i++){
for(j=0;j<(MAX-(i+1));j++){
if(list[j] > list[j+1]){
swap(&list[j],&list[j+1]);
}
}
}
}
void printlist(int list[])
{
int i;
printf("The elements of the list are: \n");
for(i=0;i<MAX;i++)
printf("%d\t",list[i]);
}
void main()
{
int list[MAX];
list[0] = 2; list[1] = 1; list[2] = 4; list[3] = 3; list[4] = 9;
list[5] = 19; list[6] = 17; list[7] = 11; list[8] = 5; list[9] = 6;
printf("The list before sorting is:\n");
printlist(list);
bsort(list);
printf("The list after sorting is:\n");
printlist(list);
}
CODE:3-CIRCULAR BUFFER
/* A circular queue example using a keyboard buffer. */
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define MAX 80
char buf[MAX+1];
int spos = 0;
int rpos = 0;
void qstore(char q);
char qretrieve(void);
int main(void)
{
register char ch;
int t;
buf[80] = '\0';
/* Input characters until a carriage return is typed. */
for(ch=' ',t=0; t<32000 && ch!='\r'; ++t) {
if(_kbhit()) {
ch = _getch();
qstore(ch);
}
printf("%d ", t);
if(ch == '\r') {
/* Display and empty the key buffer. */
printf("\n");
while((ch=qretrieve()) != '\0') printf("%c", ch);
printf("\n");
}
}
return 0;
}
/* Store characters in the queue. */
void qstore(char q)
{
if(spos+1==rpos || (spos+1==MAX && !rpos)) {
printf("List Full\n");
return;
}
buf[spos] = q;
spos++;
if(spos==MAX) spos = 0; /* loop back */
}
/* Retrieve a character. */
char qretrieve(void)
{
if(rpos==MAX) rpos = 0; /* loop back */
if(rpos==spos) return '\0';
rpos++;
return buf[rpos-1];
}
CODE 4:-/* This program displays a binary tree. */
#include <stdlib.h>
#include <stdio.h>
struct tree {
char info;
struct tree *left;
struct tree *right;
};
struct tree *root; /* first node in tree */
struct tree *stree(struct tree *root,
struct tree *r, char info);
void print_tree(struct tree *root, int l);
int main(void)
{
char s[80];
root = NULL; /* initialize the root */
do {
printf("Enter a letter: ");
gets(s);
root = stree(root, root, *s);
} while(*s);
print_tree(root, 0);
return 0;
}
struct tree *stree(
struct tree *root,
struct tree *r,
char info)
{
if(!r) {
r = (struct tree *) malloc(sizeof(struct tree));
if(!r) {
printf("Out of Memory\n");
exit(0);
}
r->left = NULL;
r->right = NULL;
r->info = info;
if(!root) return r; /* first entry */
if(info < root->info) root->left = r;
else root->right = r;
return r;
}
if(info < r->info)
stree(r, r->left, info);
else
stree(r, r->right, info);
return root;
}
void print_tree(struct tree *r, int l)
{
int i;
if(!r) return;
print_tree(r->right, l+1);
for(i=0; i<l; ++i) printf(" ");
printf("%c\n", r->info);
print_tree(r->left, l+1);
}
CODE:5-INSERTION SORT
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
void insert(char *items, int count) {
register int i, b;
char t;
for(i=1; i < count; ++i) {
t = items[i];
for(b=i-1; (b >= 0) && (t < items[b]); b--)
items[b+1] = items[b];
// items[b+1] = t;
items[b] = t;
}
}
int main(void)
{
char s[255] = "asdfasdfasdfadsfadsf";
insert(s, strlen(s));
printf("The sorted string is: %s.\n", s);
return 0;
}
CODE :6-QUEUE
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#define MAX 100
char *p[MAX], *pop(void);
int spos = 0;
int rpos = 0;
void add(void), push(char *q), print(void), remove(void);
void add(void)
{
char s[256], *p;
do {
printf("spos %d: ", spos+1);
gets(s);
if(*s==0) {
break;
}
p = (char *) malloc(strlen(s)+1);
if(!p) {
printf("Out of memory.\n");
return;
}
strcpy(p, s);
if(*s) {
push(p);
}
} while(*s);
}
void print(void)
{
int t;
for(t=rpos; t < spos; ++t)
printf("%d. %s\n", t+1, p[t]);
}
void remove(void)
{
char *p;
if((p=pop())==NULL) {
return;
}
printf("%s\n", p);
}
void push(char *q)
{
if(spos==MAX) {
printf("List Full\n");
return;
}
p[spos] = q;
spos++;
}
char *pop(void)
{
if(rpos==spos) {
printf("No more.\n");
return NULL;
}
rpos++;
return p[rpos-1];
}
int main(void)
{
char s[80];
register int t;
for(t=0; t < MAX; ++t) {
p[t] = NULL;
}
while(1) {
printf("Add(A), Print(P), Remove(R), Quit(Q): ");
gets(s);
*s = toupper(*s);
switch(*s) {
case 'A':
add();
break;
case 'P':
print();
break;
case 'R':
remove();
break;
case 'Q':
exit(0);
}
}
return 0;
}
CODE7:-QUICK SORT
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
void quickSortMain(char *items, int count);
void quickSort(char *items, int left, int right);
int main(void)
{
char s[255]="asdfasdfasdfasdfasdfasdfasdf";
quickSortMain(s, strlen(s));
printf("The sorted string is: %s.\n", s);
return 0;
}
void quickSortMain(char *items, int count)
{
quickSort(items, 0, count-1);
}
void quickSort(char *items, int left, int right)
{
int i, j;
char x, y;
i = left;
j = right;
x = items[(left+right)/2];
do {
while((items[i] < x) && (i < right))
i++;
while((x < items[j]) && (j > left))
j--;
if(i <= j) {
y = items[i];
items[i] = items[j];
items[j] = y;
i++; j--;
}
} while(i <= j);
if(i < right)
quickSort(items, i, right);
if(left < j)
quickSort(items, left, j);
}
CODE :8-QUICKSORT FOR 2D ARRAY
#include <stdio.h>
#include <string.h>
#include <assert.h>
char names[22][25] =
{
"J", "C", "I", "B", "P", "G", "D", "O", "B", "V", "C", "D", "L",
"G", "A", "K", "K", "T", "R", "J", "D", "J" };
#define NUMBER_OF_NAMES sizeof ( names ) / sizeof ( names[0] )
int main() {
int i;
/* the unsorted letter */
printf ( "The Unsorted Names.\n" );
for ( i = 0; i < NUMBER_OF_NAMES; i++ )
printf ( "%s\n", names[i] );
printf ( "Press RETURN to continue: " );
fflush ( stdout );
getchar();
qsort (( char * ) names, NUMBER_OF_NAMES, sizeof ( *names ), strcmp );
assert ( names[0][0] < names[1][0] ); /* Quick check */
/* the sorted names */
printf ( "The Sorted letter.\n" );
for ( i = 0; i < NUMBER_OF_NAMES; i++ )
printf ( "%s\n", names[i] );
}
CODE:9-QUICK SORT FOR FILE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUM_ELEMENTS 4 /* This is an arbitrary number
that should be determined
dynamically for each list. */
struct address {
char name[30];
char street[40];
char city[20];
char state[3];
char zip[11];
} ainfo;
struct address addrs[NUM_ELEMENTS] = {
"A. Alexander", "101 1st St", "Olney", "Ga", "55555",
"B. Bertrand", "22 2nd Ave", "Oakland", "Pa", "34232",
"C. Carlisle", "33 3rd Blvd", "Ava", "Or", "92000",
"D. Dodger", "4 Fourth Dr", "Fresno", "Mi", "45678"
};
void quick_disk(FILE *fp, int count);
void qs_disk(FILE *fp, int left, int right);
void swap_all_fields(FILE *fp, long i, long j);
char *get_zip(FILE *fp, long rec);
int main(void)
{
FILE *fp;
/* first, create a file to sort */
if((fp=fopen("mlist", "wb"))==NULL) {
printf("Cannot open file for write.\n");
exit(1);
}
printf("Writing unsorted data to disk.\n");
fwrite(addrs, sizeof(addrs), 1, fp);
fclose(fp);
/* now, sort the file */
if((fp=fopen("mlist", "rb+"))==NULL) {
printf("Cannot open file for read/write.\n");
exit(1);
}
printf("Sorting disk file.\n");
quick_disk(fp, NUM_ELEMENTS);
fclose(fp);
printf("List sorted.\n");
return 0;
}
/* A Quicksort for files. */
void quick_disk(FILE *fp, int count)
{
qs_disk(fp, 0, count-1);
}
void qs_disk(FILE *fp, int left, int right)
{
long int i, j;
char x[100];
i = left; j = right;
strcpy(x, get_zip(fp, (long)(i+j)/2)); /* get the middle zip */
do {
while((strcmp(get_zip(fp,i),x) < 0) && (i < right)) i++;
while((strcmp(get_zip(fp,j),x) > 0) && (j > left)) j--;
if(i <= j) {
swap_all_fields(fp, i, j);
i++; j--;
}
} while(i <= j);
if(left < j) qs_disk(fp, left, (int) j);
if(i < right) qs_disk(fp, (int) i, right);
}
void swap_all_fields(FILE *fp, long i, long j)
{
char a[sizeof(ainfo)], b[sizeof(ainfo)];
/* first read in record i and j */
fseek(fp, sizeof(ainfo)*i, SEEK_SET);
fread(a, sizeof(ainfo), 1, fp);
fseek(fp, sizeof(ainfo)*j, SEEK_SET);
fread(b, sizeof(ainfo), 1, fp);
/* then write them back in opposite slots */
fseek(fp, sizeof(ainfo)*j, SEEK_SET);
fwrite(a, sizeof(ainfo), 1, fp);
fseek(fp, sizeof(ainfo)*i, SEEK_SET);
fwrite(b, sizeof(ainfo), 1, fp);
}
/* Return a pointer to the zip code */
char *get_zip(FILE *fp, long rec)
{
struct address *p;
p = &ainfo;
fseek(fp, rec*sizeof(ainfo), SEEK_SET);
fread(p, sizeof(ainfo), 1, fp);
return ainfo.zip;
}
CODE:10- QUICK SORT FOR STRING
#include <stdio.h>
#include <string.h>
void quickSortMain(char items[][10], int count);
void quickSort(char items[][10], int left, int right);
int main(void)
{
int i;
char str[][10] = { "this","is","a","test"};
quickSortMain(str, 4);
for(i=0; i<4; i++) {
printf("%s ", str[i]);
}
return 0;
}
void quickSortMain(char items[][10], int count)
{
quickSort(items, 0, count-1);
}
void quickSort(char items[][10], int left, int right)
{
int i, j;
char *x;
char temp[10];
i = left;
j = right;
x = items[(left+right)/2];
do {
while((strcmp(items[i],x) < 0) && (i < right)) {
i++;
}
while((strcmp(items[j],x) > 0) && (j > left)) {
j--;
}
if(i <= j) {
strcpy(temp, items[i]);
strcpy(items[i], items[j]);
strcpy(items[j], temp);
i++;
j--;
}
} while(i <= j);
if(left < j) {
quickSort(items, left, j);
}
if(i < right) {
quickSort(items, i, right);
}
}
CODE:11-QUICK SORT FOR STRUCTURE
struct address {
char name[40];
char street[40];
char city[20];
char state[3];
char zip[11];
};
/* A Quicksort for structures of type address. */
void quick_struct(struct address items[], int count)
{
qs_struct(items,0,count-1);
}
void qs_struct(struct address items[], int left, int right)
{
register int i, j;
char *x;
struct address temp;
i = left; j = right;
x = items[(left+right)/2].zip;
do {
while((strcmp(items[i].zip,x) < 0) && (i < right)) i++;
while((strcmp(items[j].zip,x) > 0) && (j > left)) j--;
if(i <= j) {
temp = items[i];
items[i] = items[j];
items[j] = temp;
i++; j--;
}
} while(i <= j);
if(left < j) qs_struct(items, left, j);
if(i < right) qs_struct(items, i, right);
}
CODE 12:-SELECTION SORT
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
void select(char *items, int count);
int main(void) {
char s[255];
printf("Enter a string:");
gets(s);
select(s, strlen(s));
printf("The sorted string is: %s.\n", s);
return 0;
}
void select(char *items, int count)
{
register int a, b, c;
int exchange;
char t;
for(a = 0; a < count-1; ++a) {
exchange = 0;
c = a;
t = items[ a ];
for(b = a + 1; b < count; ++b) {
if(items[ b ] < t) {
c = b;
t = items[ b ];
exchange = 1;
}
}
if(exchange) {
items[ c ] = items[ a ];
items[ a ] = t;
}
}
}
CODE :13-SHELL SORT
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
void shell(char *items, int count)
{
int i, j, gap, k;
char x, a[5];
a[0]=8; a[1]=5; a[2]=3; a[3]=2; a[4]=1;
for(k=0; k < 5; k++) {
gap = a[k];
for(i=gap; i < count; ++i) {
x = items[i];
for(j=i-gap; (x < items[j]) && (j >= 0); j=j-gap){
items[j+gap] = items[j];
}
items[j+gap] = x;
}
}
}
int main(void)
{
char s[255]="asdfasdfasdfasdfasdf";
shell(s, strlen(s));
printf("The sorted string is: %s.\n", s);
return 0;
}
CODE:14-SYSTEM QUICK SORT
#include <stdlib.h>
#include <stdio.h>
int num[10] = {
1, 2, 6, 5, 3, 7, 9, 12, 2, 0
};
/* compare the integers */
int comp(const void *i, const void *j)
{
return *(int *)i - *(int *)j;
}
int main(void)
{
int i;
printf("Original array: ");
for(i=0; i<10; i++) {
printf("%d ", num[i]);
}
qsort(num, 10, sizeof(int), comp);
printf("Sorted array: ");
for(i=0; i<10; i++) {
printf("%d ", num[i]);
}
return 0;
}
(REMOVE CHARACTER FROM STRING)
CODE:1-(SIMPLEST CODE)
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int main ()
{
char s[] = "03264540202010210"; /* "i" from vishnu */
printf( "Before '0's removed: '%s'\n", s );
int i, j;
for( i=0; s[i]!='\0'; i++)
{
while(s[i]=='0') /* copy all chars, including NULL at end, over char to left */
{
j=i;
while(s[j]!='\0')
{
s[j]=s[j+1];
j++;
}
}
}
printf( "\nAfter '0's removed: '%s'\n", s );
}
CODE:2-
#include <stdio.h>
#define STRING "sharjeel+bilali+shaghil+bilali"
#define WHITE "+"
char *str_squeeze(char *, const char *);
char *str_tok_r(char *, const char *, char **);
size_t str_spn(const char *, const char *);
size_t str_cspn(const char *, const char *);
char *str_chr(const char *, int);
char *str_cpy(char *, const char *);
int main(void)
{
const char *const original = STRING;
char s1[sizeof STRING];
puts(str_squeeze(str_cpy(s1, original), WHITE));
return 0;
}
char *str_squeeze(char *s1, const char *s2)
{
char *p3;
char const *const p2 = s2;
char *const p1 = s1;
s2 = str_tok_r(p1, p2, &p3);
while (s2 != NULL) {
while (*s2 != '\0') {
*s1++ = *s2++;
}
s2 = str_tok_r(NULL, p2, &p3);
}
*s1 = '\0';
return p1;
}
char *str_tok_r(char *s1, const char *s2, char **p1)
{
if (s1 != NULL) {
*p1 = s1;
}
s1 = *p1 + str_spn(*p1, s2);
if (*s1 == '\0') {
return NULL;
}
*p1 = s1 + str_cspn(s1, s2);
if (**p1 != '\0') {
*(*p1)++ = '\0';
}
return s1;
}
size_t str_spn(const char *s1, const char *s2)
{
size_t n;
for (n = 0; *s1 != '\0' && str_chr(s2, *s1) != NULL; ++s1) {
++n;
}
return n;
}
size_t str_cspn(const char *s1, const char *s2)
{
size_t n;
for (n = 0; str_chr(s2, *s1) == NULL; ++s1) {
++n;
}
return n;
}
char *str_chr(const char *s, int c)
{
while (*s != (char)c) {
if (*s == '\0') {
return NULL;
}
++s;
}
return (char *)s;
}
char *str_cpy(char *s1, const char *s2)
{
char *const p1 = s1;
do {
*s1 = *s2++;
} while (*s1++ != '\0');
return p1;
}
CODE:4-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void rmchr(char s1[], char a);
void testIt(char *, char);
char s1[] = "0965041024580035010";
char s2[] = "hello, world.";
char s3[] = "zzzzz";
char s4[] = "w";
const char c1 = '0';
const char c2 = 'l';
const char c3 = 'z';
const char c4 = 'w';
int main(int argc, char *argv[]) {
testIt(s1,c1);
testIt(s2,c2);
testIt(s2,c2);
testIt(s3,c3);
testIt(s4,c4);
return 0;
}
/*
* rmchr( ): remove 'a' from the string 's'
*/
void rmchr(char s1[], char a) {
char *ptr = s1; /* Pointer to traverse array */
char *sptr; /* Pointer for skipped chars */
if (strchr(s1,a) != NULL) {
while (*ptr != '\0') {
if (*ptr == a) {
for (sptr = ptr; *sptr == a; ++sptr);
memcpy(ptr,sptr,&s1[strlen(s1)+1] - sptr);
}
if (*ptr != '\0') ++ptr;
} /* end while not end of string*/
} /* end if a is in s1 */
} /* end rmchr( ) */
void testIt(char *s, char c) {
printf("The string is \"%s\" with %c.\n", s, c);
rmchr(s, c);
printf("The string is \"%s\" without %c.\n", s, c);
puts("");
}
(CONVERT INTEGER TO STRING)
CODE:1-
#include <stdio.h>
#include <stdlib.h>
void main ()
{
int i;
char str[5];
printf ("Enter a number: ");
scanf ("%d",&i);
itoa (i,str,10);
printf ("desimal: %s\n", str);
itoa (i, str, 16);
printf ("hexadecimal: %s\n", str);
itoa (i, str, 2);
printf ("binary: %s\n", str);
return 0;
}