0% found this document useful (0 votes)
50 views7 pages

Strings

C strings are arrays of characters terminated by a null character. Strings are enclosed in double quotes and stored in char arrays. Common string functions like strcpy(), strcmp(), and strlen() in the string.h header allow copying, comparing, and getting the length of strings. Other functions like strcat() concatenate strings and strrev() reverses strings. Math operations are supported through functions in the math.h header like sqrt(), pow(), ceil(), and floor().
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views7 pages

Strings

C strings are arrays of characters terminated by a null character. Strings are enclosed in double quotes and stored in char arrays. Common string functions like strcpy(), strcmp(), and strlen() in the string.h header allow copying, comparing, and getting the length of strings. Other functions like strcat() concatenate strings and strrev() reverses strings. Math operations are supported through functions in the math.h header like sqrt(), pow(), ceil(), and floor().
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Strings

 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.

Example for C string:


 char string[20] = {‘f’, ’r’, ‘e’, ‘s’, ‘h’, ‘2’, ‘r’, ‘e’, ‘f’, ’r’, ‘e’, ‘s’, ‘h’, ‘\0’};
(or)
 char string[20] = “fresh2refresh”;
(or)
 char string [] = “fresh2refresh”;
 Difference between above declarations are, when we declare char as “string[20]”, 20
bytes of memory space is allocated for holding the string value.
 When we declare char as “string[]”, memory space will be allocated as per the
requirement during execution of the program.

char str[100];
It holds characters as you would expect: str[0] is the first character of the string, str[1] is the
second character, and so on. But why is a 100-element array unable to hold up to 100 characters?
Because C uses null-terminated strings, which means that the end of any string is marked by
null character which is represented in C as '\0'. C has to count the characters until it finds '\0'.
C provides no explicit support for strings in the language itself, all of the string-handling
functions are implemented in libraries. The string I/0 operations (gets, puts, and so on) are
implemented in <stdio.h>, and a set of fairly simple string manipulation functions are
implemented in <string.h> (on some systems, <strings.h> ).

The string library (<string.h> contains string library function called. Here is an extremely
common piece of code to find in a normal C program:

Example program for C string:


#include <stdio.h>
void main ()
{
char string[20] = "fresh2refresh.com";
printf("The string is : %s", string );
}
Output
fresh2refresh.com

C String functions:

 String.h header file supports all the string functions in C language. All the string
functions are given below.
 Each string function name below for detail description and example programs.
strcpy ( ) : Copies str2 into str1
 strcpy( ) function copies contents of one string into another string. Syntax for strcpy
function is given as : strcpy (destination, source );
 Example:
strcpy ( str1, str2) – It copies contents of str2 into str1. strcpy ( str2, str1). It copies
contents of str1 into str2.
 If destination string length is less than source string, entire source string value won’t be
copied into destination string.
 For example, consider destination string length is 20 and source string length is 30. Then,
only 20 characters from source string will be copied into destination string and remaining
10 characters won’t be copied and will be truncated.
Example program for strcpy( ) function in C:
#include <stdio.h>
#include <string.h>
void main( )
{
char source[ ] = "fresh2refresh" ;
char target[20]= "" ;
printf ( "\nsource string = %s", source ) ;
printf ( "\ntarget string = %s", target ) ;
strcpy ( target, source ) ;
printf ( "\ntarget string after strcpy( ) = %s", target ) ;
}
strncpy ( ) : Copies given number of characters of one string to another
 strncpy( ) function copies portion of contents of one string into another string. Syntax for
strncpy( ) function is given as. strncpy ( destination, source, size_t num );
 Example:
strncpy ( str1, str2, 4). It copies first 4 characters of str2 into str1.
 If destination string length is less than source string, entire source string value won’t be
copied into destination string.
 For example, consider destination string length is 20 and source string length is 30.
 If you want to copy 25 characters from source string using strncpy( ) function, only 20
characters from source string will be copied into destination string and remaining 5
characters won’t be copied and will be truncated.
Example program for strncpy( ) function in C:
#include <stdio.h>
#include <string.h>
void main( )
{
char source[ ] = "fresh2refresh" ;
char target[20]= "" ;
printf ( "\nsource string = %s", source ) ;
printf ( "\ntarget string = %s", target ) ;
strncpy ( target, source, 5 ) ;
printf ( "\ntarget string after strcpy( ) = %s", target ) ;
}
strcmp ( ) : Returns 0 if str1 is same as str2. Returns <0 if strl < str2. Returns >0 if str1 >
str2
 strcmp( ) function in C compares two given strings and returns zero if they are same.
 If length of string1 < string2, it returns < 0 value. If length of string1 > string2, it returns
> 0 value. Syntax for strcmp( ) function is given as. strcmp (str1,str2 );
 strcmp( ) function is case sensitive. i.e, “A” and “a” are treated as different characters.

You use the strcmp function in the string library to compare two strings. It returns an integer
that indicates the result of the comparison. Zero means the two strings are equal, a negative value
means that s1 is less than s2, and a positive value means s1 is greater than s2.

Example program for strcmp( ) function in C:


#include <stdio.h>
#include <string.h>
void main( )
{
char str1[ ] = "fresh" ;
char str2[ ] = "refresh" ;
int i, j, k ;
i = strcmp ( str1, "fresh" ) ;
j = strcmp ( str1, str2 ) ;
k = strcmp ( str1, "f" ) ;
printf ( "\n%d %d %d", i, j, k ) ;
}

strlen ( ) : Gives the length of the string


 strlen( ) function in C gives the length of the given string. Syntax for strlen( ) function is
given as. strlen (str );
 strlen( ) function counts the number of characters in a given string and returns the integer
value.
 It stops counting the character when null character is found. Because, null character
indicates the end of the string in C.
Other common functions in the string library include strlen, which returns the length of a string.

Example program for strlen() function in C:


#include <stdio.h>
#include <string.h>
void main( )
{
int len;
char array[20]="fresh2refresh.com" ;
len = strlen(array) ;
printf ( "\string length = %d \n" , len ) ;
}
strcat ( ) : Concatenates str2 at the end of str1 that is Appends a portion of string to
another
 strcat( ) function in C language concatenates two given strings. It concatenates source
string at the end of destination string. Syntax for strcat( ) function is given as.
strcat ( destination, source );
 Example:
strcat ( str2, str1 ) : str1 is concatenated at the end of str2.
 As you know, each string in C is ended up with null character (‘\0’).
 In strcat( ) operation, null character of destination string is overwritten by source string’s
first character and null character is added at the end of new destination string which is
created after strcat( ) operation.
Example program for strcat( ) function in C:
#include <stdio.h>
#include <string.h>
void main( )
{
char source[ ] = " fresh2refresh" ;
char target[ ]= " C tutorial" ;
printf ( "\nSource string = %s", source ) ;
printf ( "\nTarget string = %s", target ) ;
strcat ( target, source ) ;
printf ( "\nTarget string after strcat( ) = %s", target ) ;
}

strrev ( ) : Reverses the given string


 strrev( ) function reverses a given string in C language. Syntax for strrev( ) function is
given as: strrev(string);
 strrev( ) function is non standard function which may not available in standard library in
C.
Example program for strrev() function in C:
#include<stdio.h>
#include<string.h>
void main()
{
char name[30] = "Hello";
printf("String before strrev( ) : %s\n",name);
printf("String after strrev( ) : %s",strrev(name));
}

strlwr ( ) : Converts string to lowercase


 strlwr( ) function converts a given string into lowercase. Syntax for strlwr( ) function is
given as
 : strlwr(string);
 strlwr( ) function is non standard function which may not available in standard library in
C.
Example program for strlwr() function in C:
#include<stdio.h>
#include<string.h>
void main()
{
char str[ ] = "MODIFY This String To LOwer";
printf("%s\n",strlwr (str));
}
strupr ( ) : Converts string to uppercase
 strupr( ) function converts a given string into uppercase. Syntax for strupr( ) function is
given as: strupr(string);
 strupr( ) function is non standard function which may not available in standard library in
C.
Example program for strupr() function in C:
#include<stdio.h>
#include<string.h>
void main()
{
char str[ ] = "Modify This String To Upper";
printf("%s\n",strupr(str));
}
puts() function in C
puts() function is used to write a line to the output screen. In a C program, we use puts
function as below.
Declaration: puts (string)
Example:
#include <stdio.h>
#include <string.h>
void main()
{
char str[40];
strcpy(str, "This is a test string");
puts(string);
}
gets() functions in C
gets functions is used to read the string (sequence of characters) from keyboard input. In a C
program, we can read the string from standard input/keyboard as below.
Declaration: gets (string)
Example :
#include<stdio.h>
#include<string.h>
void main ()
{ char s[30];
printf("Enter the string? ");
gets(s);
printf("You entered %s",s); }
C Math
C Programming allows us to perform mathematical operations through the functions defined in
<math.h> header file. The <math.h> header file contains various methods for performing
mathematical operations such as sqrt(), pow(), ceil(), floor() etc.
C Math Functions
There are various methods in math.h header file. The commonly used functions of math.h header
file are given below.
No. Function Description
rounds up the given number. It returns the integer value which is greater
1) ceil(number)
than or equal to given number.
rounds down the given number. It returns the integer value which is less
2) floor(number)
than or equal to given number.
3) sqrt(number) returns the square root of given number.
pow(base,
4) returns the power of given number.
exponent)
5) abs(number) returns the absolute value of given number.

C Math Example

Let's see a simple example of math functions found in math.h header file.
#include<stdio.h>
#include <math.h>
void main(){
printf("\n%f",ceil(3.6));
printf("\n%f",ceil(3.3));
printf("\n%f",floor(3.6));
printf("\n%f",floor(3.2));
printf("\n%f",sqrt(16));
printf("\n%f",sqrt(7));
printf("\n%f",pow(2,4));
printf("\n%f",pow(3,3));
printf("\n%d",abs(-12));
}

Output:
4.000000
4.000000
3.000000
3.000000
4.000000
2.645751
16.000000
27.000000
12
C Preprocessor directives:
Before a C program is compiled in a compiler, source code is processed by a program called
preprocessor. This process is called preprocessing. The preprocessor will process directives
that are inserted into the C source code. These directives allow additional actions to be taken on
the C source code before it is compiled into object code. Directives are not part of the C
language itself. Preprocessor directives are lines included in a program that begin with the
character #, which make them different from a typical source code text. They are invoked by the
compiler to process some programs before compilation.

 #define – This macro defines constant value and can be any of the basic data types.
Example : #define height 100
 #include <file_name> – The source code of the file “file_name” is included in the main C
program where “#include <file_name>” is mentioned.
Example : #include <stdio.h>
exit()
exit() is a standard library function, which terminates program execution when it is called.
stdlib.h needs to be included in order to use exit().It returns the control to the operating system
or another program that uses this one as a sub-process.
#include<stdio.h>
#include<stdlib.h>
int i=0;
for(i=0; i<5; i++)
{
if(i==2)
exit(0);
printf("%d ", i);
}
In the above example, the loop continues normally for i=0, i=1 and when it reaches i=2, it
executes line exit(0); statement and your program is over. The output will be: 0,1.

You might also like