Tuesday, 2 May 2017

string

C Strings

String in C language is an array of characters that is terminated by \0 (null character).
There are two ways to declare string in c language.
  1. By char array
  2. By string literal
Let's see the example of declaring string by char array in C language.
  1. char ch[10]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};  
As you know well, array index starts from 0, so it will be represented as in the figure given below.
C Array While declaring string, size is not mandatory. So you can write the above code as given below:
  1. char ch[]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};  
You can also define string by string literal in C language. For example:
  1. char ch[]="javatpoint";  
In such case, '\0' will be appended at the end of string by the compiler.

Difference between char array and string literal

The only difference is that string literal cannot be changed whereas string declared by char array can be changed.

String Example in C

Let's see a simple example to declare and print string. The '%s' is used to print string in c language.
  1. #include <stdio.h>  
  2. void main ()  
  3. {  
  4.    char ch[11]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};  
  5.    char ch2[11]="javatpoint";  
  6.   
  7.    printf("Char Array Value is: %s\n", ch);  
  8.    printf("String Literal Value is: %s\n", ch2);  
  9. }  
Output:
Char Array Value is: javatpoint  String Literal Value is: javatpoint  

C gets() and puts() functions

The gets() function reads string from user and puts() function prints the string. Both functions are defined in <stdio.h> header file.
Let's see a simple program to read and write string using gets() and puts() functions.
  1. #include<stdio.h>  
  2. #include<conio.h>  
  3. void main(){  
  4.     char name[50];  
  5.     clrscr();  
  6.     printf("Enter your name: ");  
  7.     gets(name); //reads string from user  
  8.     printf("Your name is: ");  
  9.     puts(name);  //displays string  
  10.     getch();  
  11. }  
Output:
Enter your name: Sonoo Jaiswal  Your name is: Sonoo Jaiswal  
S.N. Function & Purpose
1 strcpy(s1, s2);
Copies string s2 into string s1.
2 strcat(s1, s2);
Concatenates string s2 onto the end of string s1.
3 strlen(s1);
Returns the length of string s1.
4 strcmp(s1, s2);
Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
5 strchr(s1, ch);
Returns a pointer to the first occurrence of character ch in string s1.
6 strstr(s1, s2);
Returns a pointer to the first occurrence of string s2 in string s1.

C String Length: strlen() function

The strlen() function returns the length of the given string. It doesn't count null character '\0'.
  1. #include <stdio.h>  
  2. void main()  
  3. {  
  4.    char ch[20]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};  
  5.    printf("Length of string is: %d",strlen(ch));  
  6. }  
Output:
Length of string is: 10  

C Copy String: strcpy()

The strcpy(destination, source) function copies the source string in destination.
  1. #include <stdio.h>  
  2. void main()  
  3. {  
  4.    char ch[20]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};  
  5.    char ch2[20];  
  6.    strcpy(ch2,ch);  
  7.    printf("Value of second string is: %s",ch2);  
  8. }  
Output:
Value of second string is: javatpoint  

C String Concatenation: strcat()

The strcat(first_string, second_string) function concatenates two strings and result is returned to first_string.
  1. #include <stdio.h>  
  2. void main()  
  3. {  
  4.    char ch[10]={'h', 'e', 'l', 'l', 'o', '\0'};  
  5.    char ch2[10]={'c', '\0'};  
  6.    strcat(ch,ch2);  
  7.    printf("Value of first string is: %s",ch);  
  8. }  
Output:
Value of first string is: helloc  

C Compare String: strcmp()

The strcmp(first_string, second_string) function compares two string and returns 0 if both strings are equal.
Here, we are using gets() function which reads string from the console.
  1. #include <stdio.h>  
  2. void main()  
  3. {  
  4.    char str1[20],str2[20];  
  5.   printf("Enter 1st string: ");  
  6.   gets(str1);//reads string from console  
  7.   printf("Enter 2nd string: ");  
  8.   gets(str2);  
  9.   if(strcmp(str1,str2)==0)  
  10.       printf("Strings are equal");  
  11.   else  
  12.       printf("Strings are not equal");  
  13. }  
Output:
Enter 1st string: hello  Enter 2nd string: hello  Strings are equal  

C Reverse String: strrev()

The strrev(string) function returns reverse of the given string. Let's see a simple example of strrev() function.
  1. #include<stdio.h>  
  2. #include<conio.h>  
  3. void main(){  
  4.   char str[20];  
  5.   clrscr();  
  6.   printf("Enter string: ");  
  7.   gets(str);//reads string from console  
  8.   printf("String is: %s",str);  
  9.   printf("\nReverse String is: %s",strrev(str));  
  10.   getch();  
  11. }    
Output:
Enter string: javatpoint  String is: javatpoint  Reverse String is: tnioptavaj  

C String Lowercase: strlwr()

The strlwr(string) function returns string characters in lowercase. Let's see a simple example of strlwr() function.
  1. #include<stdio.h>  
  2. #include<conio.h>  
  3. void main(){  
  4.   char str[20];  
  5.   clrscr();  
  6.   printf("Enter string: ");  
  7.   gets(str);//reads string from console  
  8.   printf("String is: %s",str);  
  9.   printf("\nLower String is: %s",strlwr(str));  
  10.   getch();  
  11. }    
Output:
Enter string: JAVATpoint  String is: JAVATpoint  Lower String is: javatpoint  

C String Uppercase: strupr()

The strupr(string) function returns string characters in uppercase. Let's see a simple example of strupr() function.
  1. #include<stdio.h>  
  2. #include<conio.h>  
  3. void main(){  
  4.   char str[20];  
  5.   clrscr();  
  6.   printf("Enter string: ");  
  7.   gets(str);//reads string from console  
  8.   printf("String is: %s",str);  
  9.   printf("\nUpper String is: %s",strupr(str));  
  10.   getch();  
  11. }    
Output:
Enter string: javatpoint  String is: javatpoint  Upper String is: JAVATPOINT  

C String strstr()

The strstr() function returns pointer to the first occurrence of the matched string in the given string. It is used to return substring from first match till the last character.
Syntax:
  1. char *strstr(const char *string, const char *match)  

String strstr() parameters

string: It represents the full string from where substring will be searched.
match: It represents the substring to be searched in the full string.

String strstr() example

  1. #include<stdio.h>  
  2. #include<conio.h>  
  3. #include<string.h>  
  4. void main(){  
  5.   char str[100]="this is javatpoint with c and java";  
  6.   char *sub;  
  7.   clrscr();  
  8.   sub=strstr(str,"java");  
  9.   printf("\nSubstring is: %s",sub);  
  10.   getch();  
  11. }  
Output:
javatpoint with c and java  



No comments:

Post a Comment

Please write your view and suggestion....