Sunteți pe pagina 1din 9

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.

How to declare a string?


Before you can work with strings, you need to declare them first. Since string is an array of
characters. You declare strings in a similar way like you do with arrays.

Here's how you declare a string:

char s[5];

How to initialize strings?


You can initialize strings in a number of ways.

char c[] = "abcd";


char c[50] = "abcd";
char c[] = {'a', 'b', 'c', 'd', '\0'};
char c[5] = {'a', 'b', 'c', 'd', '\0'};
Read String from the user
You can use the scanf() function to read a string.The scanf() function reads the sequence of
characters until it encounters a whitespace (space, newline, tab etc.).
Example 1: scanf() to read a string
#include <stdio.h>

int main()
{
char name[20];

printf("Enter name: ");


scanf("%s", name);
printf("Your name is %s.", name);

return 0;
}

Output

Enter name: Dr.K.Kartheeban


Your name is Dr.K.Kartheeban.

#include <stdio.h>
#include <string.h>
int main()
{
/* String Declaration*/
char nickname[20];

printf("Enter your Nick name:");

/* I am reading the input string and storing it in nickname


* Array name alone works as a base address of array so
* we can use nickname instead of &nickname here
*/
scanf("%s", nickname);

/*Displaying String*/
printf("%s",nickname);

return 0;
}

How to read a line of text?


You can use gets() function to read a line of string. And, you can use puts() to display the string.

Example 2: gets() and puts()


#include <stdio.h>
int main()
{
char name[30];
printf("Enter name: ");
gets(name); // read string
printf("Name: ");
puts(name); // display string
return 0;
}
When you run the program, the output will be:

Enter name: Tom Hanks


Name: Tom Hanks

Passing Strings to Function


Strings can be passed to a function in a similar way as arrays. Learn more about passing array to
a function.

Example 3: Passing string to a Function


1. #include <stdio.h>
2. void displayString(char str[]);
3.
4. int main()
5. {
6. char str[50];
7. printf("Enter string: ");
8. gets(str);
9. displayString(str); // Passing string to a function.
10. return 0;
11. }
12. void displayString(char str[])
13. {
14. printf("String Output: ");
15. puts(str);
16. }
C STRING FUNCTIONS:

C programming language provides a set of pre-defined functions called string handling


functions to work with string values. The string handling functions are defined in a header
file called string.h. Whenever we want to use any string handling function we must include
the header file called string.h.

The following table provides most commonly used string handling function and their use...

Function Syntax (or) Example Description

strcpy() strcpy(string1, string2) Copies string2 value into string1

strncpy() strncpy(string1, string2, 5) Copies first 5 characters string2 into string1

strlen() strlen(string1) returns total number of characters in string1

strcat() strcat(string1,string2) Appends string2 to string1

strncat() strncpy(string1, string2, 4) Appends first 4 characters of string2 to string1

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())

strlwr() strlwr(string1) Converts all the characters of string1 to lower case.

strupr() strupr(string1) Converts all the characters of string1 to upper case.

strdup() string1 = strdup(string2) Duplicated value of string2 is assigned to string1

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

strrev() strrev(string1) It reverses the value of string1

/*C example of strlen().*/


#include <stdio.h>
#include <string.h>

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() - String Upper and strlwr() - String Lower

strupr converts string into uppercase. strlwr converts string into lowercase.

/*C example of strupr() and strlwr().*/


#include<stdio.h>
#include<string.h>

int main()
{
char str[]="Hello Wolrd.";

printf("Value of str: %s\n",str);

//convert into uppercase


strupr(str);
printf("Value of str (Uppercase): %s\n",str);

//convert into lowercase


strupr(str);
printf("Value of str (Lowercase): %s\n",str);
return 0;
}
Value of str: Hello World.
Value of str (Uppercase): HELLO WORLD.
Value of str (Lowercase): hello world.

/* C example program of strrev().*/

strrev() - String Reverse

strrev reverses the string.

#include<stdio.h>
#include<string.h>

int main()
{
char str[30];

printf("Enter string: ");


gets(str);

printf("Entered string is: %s\n",str);

//Reverse string
strrev(str);
printf("Reversed string is: %s\n",str);

return 0;
}

Enter string: Hello World


Entered string is: Hello World
Reversed string is: dlroW olleH

strcpy() - String Copy

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.

/* C example program of strcpy().*/


#include<stdio.h>
#include<string.h>

int main()
{
char str1[30];
char str2[30];

printf("Enter string 1: ");


gets(str1);

//copy str1 into str2


strcpy(str2,str1);
printf("str1: %s \nstr2: %s \n",str1,str2);

return 0;
}
Enter string 1: Hello World.
str1: Hello World.
str2: Hello World.

6) strcmp() - String Compare


strcmp compares two strings and return 0, less than 0 and greater than 0
based on strings, if strings are same function will return 0, other function will
return difference of first dissimilar character, difference may be positive or
negative.

7) strcmpi() - String Comparing Ignoring case


strcmpi compares two strings ignoring case sensitivity and return 0, less than
0 and greater than 0 based on strings, if strings are same function will return 0,
other function will return difference of first dissimilar character, difference may
be positive or negative.
/* C example program of strcmp() and strcmpi().*/
#include<stdio.h>
#include<string.h>

int main()
{
char str1[30];
char str2[30];

printf("Enter string1: "); gets(str1);


printf("Enter string2: "); gets(str2);

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

8) strcat() - String Concatenate

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.

/* C example program of strcat() .*/

#include<stdio.h>
#include<string.h>

int main()
{
char title[5],fName[30],lName[30];
char name[100]={0}; //assign null

printf("Enter title (Mr./Mrs.): "); gets(title);


printf("Enter first name: "); gets(fName);
printf("Enter last name: "); gets(lName);

//create complete name using string concatenate


strcat(name,title);
strcat(name," ");

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

9) strncpy() - String Copy with finite number of


characters
strncpy copies first N characters. This function will copy first N characters
from second parameter’s value (variable) in first parameter’s variable.
/*C example of strncpy().*/
#include <stdio.h>
#include <string.h>

int main()
{
char str1[30]={0};
char str2[30]={0};

printf("Enter string1: "); gets(str1);

//copy first 3 characters of str1


//before copy insert null in target string

strncpy(str2,str1,3);

printf("After copying str2 is: %s\n",str2);

return 0;
}

Enter string1: Hello


After copying str2 is: Hel

S-ar putea să vă placă și