#include <stdio.h>
#include <string.h>
int main( )
{
char source[ ] = “ ftl” ;
char target[ ]= “ welcome to” ;
printf (“\n Source string = %s”, source ) ;
printf ( “\n Target string = %s”, target ) ;
strcat ( target, source ) ;
printf ( “\n Target string after strcat( ) = %s”, target ) ;
}
Source string = ftl
Target string = welcome to
Target string after strcat() = welcome to ftl
strncat( ) function in C language concatenates (appends) portion of one string at the end of another string.
Syntax : char * strncat ( char * destination, const char * source, size_t num );
Example :
strncat ( str2, str1, 3 ); – First 3 characters of str1 is concatenated at the end of str2.
strncat ( str1, str2, 3 ); - First 3 characters of str2 is concatenated at the end of str1.
As you know, each string in C is ended up with null character (‘\0′).
In strncat( ) 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 strncat( ) operation.
#include <stdio.h>
#include <string.h>
int main( )
{
char source[ ] =”" ftl” ;
char target[ ]= “welcome to” ;
printf ( “\n Source string = %s”, source ) ;
printf ( “\n Target string = %s”, target ) ;
strncat ( target, source, 3 ) ;
printf ( "”\n Target string after strncat( ) = %s”, target ) ;
}
Source string = ftl
Target string = welcome to
Target string after strncat()= welcome to ft
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 : int strcmp ( const char * str1, const char * str2 );
strcmp( ) function is case sensitive. i.e., “A” and “a” are treated as different characters.
Example :
char city[20] = “Madras”;
char town[20] = “Mangalore”;
strcmp(city, town);
This will return an integer value “-10‟ which is the difference in the ASCII values of the first mismatching letters “D‟ and “N‟.
* Note that the integer value obtained as the difference may be assigned to an integer variable as follows:
int n;
n = strcmp(city, town);
strcmpi( ) function in C is same as strcmp() function. But, strcmpi( ) function is not case sensitive. i.e., “A” and “a” are treated as same characters. Whereas, strcmp() function treats “A” and “a” as different characters.
• strcmpi() function is non standard function which may not available in standard library.
• Both functions compare 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.
strcmp( ) function is case sensitive. i.e., “A” and “a” are treated as different characters.
Syntax : int strcmpi ( const char * str1, const char * str2 );
Example :
m=strcmpi(“ DELHI ”, “ delhi ”); m = 0.
strlwr() function converts a given string into lowercase.
Syntax : char *strlwr(char *string);
strlwr() function is non standard function which may not available in standard library in C.
#include<stdio.h>
#include<string.h>
int main()
{
char str[ ] = “MODIFY This String To Lower”;
printf(“%s\n”, strlwr (str));
return 0;
}
modify this string to lower
strupr() function converts a given string into uppercase.
Syntax : char *strupr(char *string);
strupr() function is non standard function which may not available in standard library in C.
#include<stdio.h>
#include<string.h>
int main()
{
char str[ ] = “Modify This String To Upper”;
printf(“%s\n”, strupr(str));
return 0;
}
MODIFY THIS STRING TO UPPER
strrev() function reverses a given string in C language.
Syntax : char *strrev(char *string);
strrev() function is non standard function which may not available in standard library in C.
Example :
char name[20]=”ftl”; then
strrev(name)= ltf
#include<stdio.h>
#include<string.h>
int main()
{
char name[30] = “Hello”;
printf(“String before strrev( ) : %s\n”, name);
printf(“String after strrev( ) : %s”, strrev(name));
return 0;
}
String before strrev( ) : Hello
String after strrev( ) : olleH
strchr() function returns pointer to the first occurrence of the character in a given string.
Syntax : char *strchr(const char *str, int character);
#include <stdio.h>
#include <string.h>
int main ()
{
char string[25] =”This is a string “;
char *p;
p = strchr (string,'i');
printf (“Character i is found at position %d\n”,p-string+1);
printf (“First occurrence of character \”i\” in \”%s\” is” \” \”%s\””,string, p);
return 0;
}
Character i is found at position 3
First occurrence of character “i” in “This is a string” is “is is a string”
strstr( ) function returns pointer to the first occurrence of the string in a given string.
Syntax : char *strstr(const char *str1, const char *str2);
#include <stdio.h>
#include <string.h>
int main( )
{
char string[55] =”This is a test string for testing”;
char *p;
p = strstr (string, ”test”);
if(p)
{
printf(“string found\n” );
printf (“First occurrence of string \”test\” in \”%s\” is”\” \”%s\””,string, p);
}
else printf(“string not found\n” );
return 0;
}
String found
First occurrence of ”test” in “this is a test string for testing” is “testing string for testing”
It converts string-value to numeric-value and it converts a numeric-string value to equivalent integer-value.
Syntax : int atoi(string);
Example :
printf(“output=%d”, atoi(“123”)+atoi(“234”));
This printf() will print 357
converts a long int string value to equivalent long integer value.
Syntax : long int atol(string);
Example :
printf(“output=%d”, atol(“486384”)-atol(“112233”));
This statement will print 374151
converts a floating point text format value to double value.
Syntax : int atoi(string);
Example :
printf(“%1f”,atof(“3.1412”)*5*5);
This statement will print 78.530000
These functions converts a given number(int/long int/unsigned long int) to equivalent text format based on the given numbering system radix value.
These functions take three arguments, the numeric value, target string address in which the value to be stored and radix value. Finally returns the target string address, so that function-call can be used as argument/expression.
Syntax : char* itoa(int value, char *targetstringaddress, int radix );
Example :
Char temp[50];
Printf(“output=%s”, itoa(45,temp,2));
Output : 101101
# include<stdio.h>
# include<conio.h>
# include<string.h>
main( )
{
char st[80], ch;
int count = 0, i;
clrscr( );
printf(“ \n Enter the sentence: \n”);
gets(st);
for( i=0; i<strlen(st); i++)
switch(st [i ])
{
case ‘A’:
case ‘E’:
case ‘I’:
case ‘O’:
case ‘U’:
case ‘a’:
case ‘e’:
case ‘I’:
case ‘o’:
case ‘u’:
count ++;
break;
}
printf(“\n %d vowels are present in the sentence”, count);
getch( );
}
• When this program is executed, the user has to enter the sentence.
• Note that gets( ) function is used to read the sentence because the string has white spaces between the words.
• The vowels are counted using a switch statement in a loop.
• Note that a count++ statement is given only once to execute it for the cases in the switch statement.
Enter the sentence :
This is a book
5 vowels are present in the sentence.
# include<stdio.h>
# include<string.h>
# include<conio.h>
main()
{
char txt[250], ch, st[30];
int ins, wds, chs, i;
printf(“ \n Enter the text, type $ st end \n \n”);
i=0;
while((txt[i++]= getchar( ) ) ! =’$’);
i--;
st[ i ] = ‘\0’;
ins = wds = chs = 0;
i=0;
while(txt[ i ]!=’$’)
{
switch(txt[ i ])
{
case ‘,’:
case ‘!’:
case ‘\t’:
case ‘ ‘:
{
wds ++;
chs ++;
break;
}
case ‘?’:
case ‘.’:
{
wds ++;
chs ++;
break;
}
default:chs ++;
break;
}
i++;
}
printf(“\n\n no of char (incl.blanks) = %d”, chs);
printf(“\n No. of words = %d”, wds);
printf(“\n No of lines = %d”, ins);
getch() ;
}
Enter the text, type $ at end
What is a string? How do you initialize it? Explain with example.
With example: $
No of char: (inch. Blanks) = 63
No of words = 12
No of lines =1.