Today ,I wrote two programs one for C string functions and other one for numeric format conversions in C.First, we will discuss string functions which are used frequently in programs.Here are some important C functions:-
Here is the program for the same functions:-
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char str1[] = "amar";
char stra[] = "amar123";
char strb[] = "12334amar";
char str10[] = "my,name.is-amar";
char str2[40] ;
char str3[40];
char str4[40];
char str5[40];
char str6[40] = "singh";
char str7[40] ="hi";
char str8[40] = "bye" ;
char str9[40] ;
char str11[40];
char *ch = strchr(str6 ,'n');
char *ch1 = strrchr(str1 ,'a');
char keys[] = "1234567890";
char vowel[] ="aeiou";
int i = strcspn(stra,keys);
int j = strspn(strb,keys);
//cpoy
strcpy(str3,str1);
strcpy(str2,"hi");
strncpy(str4,str6,sizeof(str6));
strncpy(str5,str1,3);
strcat(str6,str1);
strncat(str7,str1,3);
strxfrm(str8,str1,3);
memcpy(str9,str6,sizeof(str6));
memset(str10,97,sizeof(str10));
memmove(str10+5,str10+3,10);
int k=memcmp(strb,stra,sizeof(stra));
char* ch2 = strpbrk(str6,vowel);
printf("str10 = %s\n",str10);
printf("str2 = %s\n",str2);
printf("str3 = %s\n",str3);
printf("str4 = %s\n",str4);
printf("str5 = %s\n",str5);
printf("str6 = %s\n",str6);
printf("str7 = %s\n",str7);
printf("str8 = %s\n",str8);
printf("str9 = %s\n",str9);
printf("str10 = %s\n",str10);
printf("k = %d\n",k);
printf("strlen = %d\n",strlen(str1));
printf("strcmp = %d\n",strcmp(str1,str6));
printf("strncmp = %d\n",strncmp(str7,str5,3));
printf("strcoll = %d\n",strcoll(str7,str5));
printf("ch = %u\n",ch-str6+1);
printf("ch1 = %u\n",ch1-str1+1);
printf("i = %u\n",i+1);
printf("j = %u\n",j);
printf("ch2=%c\n",*ch2);
_getch();
}
Output of the program:--
Here is the program for the same functions:-
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char str1[] = "amar";
char stra[] = "amar123";
char strb[] = "12334amar";
char str10[] = "my,name.is-amar";
char str2[40] ;
char str3[40];
char str4[40];
char str5[40];
char str6[40] = "singh";
char str7[40] ="hi";
char str8[40] = "bye" ;
char str9[40] ;
char str11[40];
char *ch = strchr(str6 ,'n');
char *ch1 = strrchr(str1 ,'a');
char keys[] = "1234567890";
char vowel[] ="aeiou";
int i = strcspn(stra,keys);
int j = strspn(strb,keys);
//cpoy
strcpy(str3,str1);
strcpy(str2,"hi");
strncpy(str4,str6,sizeof(str6));
strncpy(str5,str1,3);
strcat(str6,str1);
strncat(str7,str1,3);
strxfrm(str8,str1,3);
memcpy(str9,str6,sizeof(str6));
memset(str10,97,sizeof(str10));
memmove(str10+5,str10+3,10);
int k=memcmp(strb,stra,sizeof(stra));
char* ch2 = strpbrk(str6,vowel);
printf("str10 = %s\n",str10);
printf("str2 = %s\n",str2);
printf("str3 = %s\n",str3);
printf("str4 = %s\n",str4);
printf("str5 = %s\n",str5);
printf("str6 = %s\n",str6);
printf("str7 = %s\n",str7);
printf("str8 = %s\n",str8);
printf("str9 = %s\n",str9);
printf("str10 = %s\n",str10);
printf("k = %d\n",k);
printf("strlen = %d\n",strlen(str1));
printf("strcmp = %d\n",strcmp(str1,str6));
printf("strncmp = %d\n",strncmp(str7,str5,3));
printf("strcoll = %d\n",strcoll(str7,str5));
printf("ch = %u\n",ch-str6+1);
printf("ch1 = %u\n",ch1-str1+1);
printf("i = %u\n",i+1);
printf("j = %u\n",j);
printf("ch2=%c\n",*ch2);
_getch();
}
Output of the program:--
str10 = aaaaaaaaaaaaaaaa
str2 = hi
str3 = amar
str4 = singh
str5 = ama
str6 = singhamar
str7 = hiama
str8 = ama
str9 = singhamar
str10 = aaaaaaaaaaaaaaaa╠╠╠╠╠╠╠╠12334amar
k = -1
strlen = 4
strcmp = -1
strncmp = 7
strcoll = 1
ch = 3
ch1 = 3
i = 5
j = 5
ch2=i
Numeric format conversion easily converts one format of datatype to other format of datatype.It uses various
functions which are following:-
Here is the program for the numeric formats .Here are some functions for C99 which are not used in my program and some are not supported:-
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
int i1=323,i2=345,i3=890,i4;
unsigned long int uli;
long int li,li1,li2,li3,li4,li5;
char ch1='a',ch2=99,ch3=0;
char str1[40]="345.88";
char str2[40]="36467";
char str3[40]="654647868";
char str4[40]="32.64 16.32";
char str5[40]="32.23424";
char str6[100]="684069 35a5c7 -10101011 0xe72878 010";
char *chend;
char *chend2;
float f1 = 3263.809;
float f2 = 668.098;
float f3 = 676.8798;
i4=atoi(str2);
printf("i4 = %d\n",i4);
li = atol(str3);
uli = strtoul(str2,NULL,0);
li1 = strtol(str6,&chend,10);
li2 = strtol(chend,&chend,16);
li3 = strtol(chend,&chend,2);
li4 = strtol(chend,&chend,16);
li5 = strtol(chend,NULL,8);
printf("li1 = %ld\n",li1);
printf("li2 = %ld\n",li2);
printf("li3 = %ld\n",li3);
printf("li4 = %ld\n",li4);
printf("li5 = %ld\n",li5);
double d2 = strtod(str4,&chend);
double d3 = strtod(chend ,NULL);
printf("d2 = %lf\n",d2);
printf("d3 = %lf\n",d3);
printf("uli=%lu\n",uli);
printf("li=%ld\n",li);
double d1 = atof(str5);
printf("d1=%lf\n",d1);
_getch();
}
Output:-
i4 = 36467
li1 = 684069
li2 = 3515847
li3 = -171
li4 = 15149176
li5 = 8
d2 = 32.640000
d3 = 16.320000
uli=36467
li=654647868
d1=32.234240