C Programming Strings
C Programming Strings
In C programming, a string is an array of characters terminated with a null character \0. For example:
"c string"
When compiler encounters a sequence of characters enclosed in the double quotation marks, it
appends a null character \0 at the end.
char s[5];
int main()
{
char name[20];
return 0;
}
Output
#include <stdio.h>
#include <string.h>
int main()
{
/* String Declaration*/
char nickname[20];
/*Displaying String*/
printf("%s",nickname);
return 0;
}
The following table provides most commonly used string handling function and their use...
strcmp() strcmp(string1, string2) Returns 0 if string1 and string2 are the same;
less than 0 if string1<string2; greater than 0 if string1>string2
strncmp() strncmp(string1, string2, 4) Compares first 4 characters of both string1 and string2
strcmpi() strcmpi(string1,string2) Compares two strings, string1 and string2 by ignoring case (upper or lower)
stricmp() stricmp(string1, string2) Compares two strings, string1 and string2 by ignoring case (similar to strcmpi())
strchr() strchr(string1, 'b') Returns a pointer to the first occurrence of character 'b' in string1
strrchr() 'strrchr(string1, 'b') Returns a pointer to the last occurrence of character 'b' in string1
Function Syntax (or) Example Description
strstr() strstr(string1, string2) Returns a pointer to the first occurrence of string2 in string1
strset() strset(string1, 'B') Sets all the characters of string1 to given character 'B'.
strnset() strnset(string1, 'B', 5) Sets first 5 characters of string1 to given character 'B'.
int main()
{
char str[]="www.includehelp.com";
int length;
//string length
length=strlen(str);
printf("String Length: %d\n",length);
return 0;
}
String Length: 19
strupr converts string into uppercase. strlwr converts string into lowercase.
int main()
{
char str[]="Hello Wolrd.";
#include<stdio.h>
#include<string.h>
int main()
{
char str[30];
//Reverse string
strrev(str);
printf("Reversed string is: %s\n",str);
return 0;
}
strcpy copies one string to another string, in this function there will be two parameters second
parameter’s values will be copied into first parameter’s variable.
int main()
{
char str1[30];
char str2[30];
return 0;
}
Enter string 1: Hello World.
str1: Hello World.
str2: Hello World.
int main()
{
char str1[30];
char str2[30];
//using strcmp
printf("Using strcmp:\n");
if(strcmp(str1,str2)==0)
printf("strings are same.\n");
else
printf("strings are not same.\n");
//using strcmp
printf("Using strcmpi:\n");
if(strcmpi(str1,str2)==0)
printf("strings are same.\n");
else
printf("strings are not same.\n");
return 0;
}
Enter string1: Hello World
Enter string2: hello world
Using strcmp:
strings are not same.
Using strcmpi:
strings are same.
strcat concatenates second string with first string. This function will take two parameters after
executing this function, value of second parameter will be concatenated with first string.
#include<stdio.h>
#include<string.h>
int main()
{
char title[5],fName[30],lName[30];
char name[100]={0}; //assign null
strcat(name,fName);
strcat(name," ");
strcat(name,lName);
strcat(name," ");
printf("Hi.... %s\n",name);
return 0;
}
Enter title (Mr./Mrs.): Mr.
Enter first name: Karthee
Enter last name: ban
Hi.... Mr. Karthee
int main()
{
char str1[30]={0};
char str2[30]={0};
strncpy(str2,str1,3);
return 0;
}