Showing posts with label STRINGS. Show all posts
Showing posts with label STRINGS. Show all posts
Wednesday, 23 November 2016
Saturday, 8 October 2016
atof() string to real conversion
atof() function: atof()
function converts a string of digits into a real value. The general format of the atof() function is:
Syntax: float
atof(string);
Function
accepts a string as an argument and returns the result value as a real value.
/* EXAMPLE PROGRAM FOR atof()
FUNCTION*/
#include <stdio.h>
#include <stdlib.h>
main()
{
char a[10] = "3.14";
printf("Value of pi = %f \n", atof(a));
}
ltoa() long integer to string conversion
ltoa()
function: ltoa()
function converts a long integer value into a string. The general format of the ltoa() function is:
Syntax: char
* ltoa(long int , char[] , int);
Where,
First
argument is a long integer value used for conversion.
Second
argument is a string used to store the conversion value.
Third
argument is an integer value that represents format of conversion such as
decimal, octal and hexa-decimal format.
/* EXAMPLE PROGRAM FOR atol() FUNCTION */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
main()
{
long a=10000000;
char buffer[50];
ltoa(a,buffer,2); // here 2 means binary
printf("Binary value = %s\n", buffer);
ltoa(a,buffer,10); // here 10 means decimal
printf("Decimal value = %s\n", buffer);
ltoa(a,buffer,16); // here 16 means Hexadecimal
printf("Hexadecimal value = %s\n", buffer);
}
atol() string to long integer conversion
atol()
function: atol()
function converts a string of digits into a long integer value. The general format of the atol() function is:
Syntax: long
int atol(string);
Function
accepts a string as an argument and returns the result value as a long integer.
/* EXAMPLE PROGRAM FOR atol()
FUNCTION*/
#include <stdio.h>
#include <stdlib.h>
main()
{
char a[20] = "100000000000";
printf(" Value = %ld ", atol(a));
}
itoa() integer to string conversion
itoa()
function: atoi()
function converts an integer value into a string. The general format of the itoa() function is:
Syntax: char
* itoa(int , char[] , int);
Where,
- First argument is an integer value used for conversion.
- Second argument is a string used to store the conversion value.
- Third argument is an integer value that represents format of conversion such as decimal, octal and hexa-decimal format.
atoi() string to number conversion
atoi()
function: atoi()
function converts a string of digits into an integer value. The general format of the atoi() function is:
Syntax: int
atoi(string);
Function
accepts a string as an argument and returns the result value as an integer.
#include<stdio.h>
#include<stdlib.h>
main ()
{
int i;
char buffer [256];
printf ("Enter a number: ");
fgets (buffer, 256, stdin);
i = atoi (buffer);
printf ("The value entered is %d.",i);
}
Friday, 7 October 2016
strings definition, declaration, initilization
STRINGS
- · A string is collection of "characters" (or) "array of characters"
- · C Strings are nothing but array of characters ended with null character (‘\0’).
- · This null character indicates the end of the string.
- · Strings are always enclosed by double quotes. Whereas, character is enclosed by single quotes in C.
Declaration:
Syntax : char
StringName [size];
Example : char
str[100];
The general format of initializing a string is:
Syntax : char
stringname[size] = “string value”;
- Here, collection of characters is placed within double quotation marks and termed as string constant.
Example : char str[50] = “Language in 1972”;
- Strings can also be initialized in the form of character array. In such cases, explicitly it is necessary to add the NULL character (‘\0’) at the end of the string value.
Syntax : char
stringname[size] = { ‘value1’ , ‘value2’, … , ‘\0’};
Example : char
str[10] = {‘H’ , ‘E’ , ‘L’ , ‘L’ , ‘O’ , ‘\0’};
STRING
INPUT/OUTPUT FUNCTIONS
These gets() and puts() functions are used for reading and
print an entire line as a string including blank spaces. These library functions information is
available in stdio.h header file.
gets()
function: gets() function used for reading an entire line as a string including
blank spaces.
Syntax : gets(string);
puts()
function: puts() function used for printing an entire line as a string including
blank spaces.
Syntax : puts(string);
/* EXAMPLE PROGRAM FOR READING AND
PRINT A STRING */
#include<stdio.h>
#include<conio.h>
main()
{
char
str[50];
clrscr();
printf("\nEnter
a line of text:");
gets(str);
printf("\n RESULT1:");
puts(str);
}
strcmpi() string handling functions
C- strcmpi()
function
- It refers to string comparison ignore case function.
The general
format of strcmpi() function is:
Syntax: strcmpi(string1
, string2);
Function is
used to compare the given two strings without case consideration.
/* PROGRAM TO COMPARE THE GIVEN TWO
STRINGS WITHOUT CASE */
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char
str1[50],str2[50];
clrscr();
printf("\nEnter
string 1:");
gets(str1);
printf("\nEnter
string 2:");
gets(str2);
if((strcmpi(str1,str2))==0)
printf("\nBOTH STRIGNS ARE SAME");
else
printf("\nBOTH STRINGS ARE DIFFERENT");
}
strncmpi() string handling functions
C - strncmpi()
function: The general format of strncmpi()
function is:
Syntax: strncmpi(string1
, string2 , n);
Where, n is an integer argument. Function compares atmost n characters of the given
two strings without case consideration.
/* EXAMPLE PROGRAM */
#include<string.h>
main()
{
char
str1[50],str2[50];
clrscr();
printf("\nEnter
string 1:");
gets(str1);
printf("\nEnter
string 2:");
gets(str2);
if((strncmpi(str1,str2,3))==0)
printf(“\n
Both Are Same”);
else
printf(“\n
Both Are Different”);
}
strncmp() string handling functions
C - strncmp()
function: The
general format of strncmp() function is:
Syntax: strncmp(string1
, string2 , n);
Where, n is an integer argument. Function compares atmost n characters of the
given two strings.
/* EXAMPLE PROGRAM */
#include<string.h>
main()
{
char
str1[50],str2[50];
clrscr();
printf("\nEnter
string 1:");
gets(str1);
printf("\nEnter
string 2:");
gets(str2);
if((strncmp(str1,str2,3))==0)
printf(“\n
Both Are Same”);
else
printf(“\n
Both Are Different”);
printf("\nRESULT:%d",strncmp(str1,str2,3));
}
strncpy() string handling functions
C- strncpy(): The general
format of strncpy() function is:
Syntax: strncpy(targetstring
, sourcestring , n);
Where, n is
an integer argument. Function copies
atmost n characters of source string to target string.
/* EXAMPLE PROGRAM */
#include<string.h>
main()
{
char
str1[50],str2[50];
clrscr();
printf("\nEnter
string 1:");
gets(str1);
strncpy(str2,str1,3);
str2[3]='\0';
printf("\nRESULT
1:");
puts(str1);
printf("\nRESULT
2:");
puts(str2);
}





