String in C





String is a collection of characters.

String:

  • In C programming, A string is an array of characters which is terminated by null character ( '\0' ).
  • Each character in array occupies one byte of memory and the last character is always null character ( '\0' ) {backslash zero}.
  • for e.g:
    char s [6] = {'H','E','L','L','O','\0'};
           OR
    char s [ ] = {'H','E','L','L','O','\0'};
           OR
    char s [6] = "HELLO";
           OR
    char s [ ] = "HELLO";

    The above string can be represented as:

    prowessapps.in
  • NOTE:

    If you initialize a string by specifying character by character at every index in array (as in first example) then you must have to put null ( '\0' ) character at the end yourself.
    But, if you initialize a string by string literal (providing group of characters between " ") then null character ( '\0' ) is automatically placed by C compiler at the end.

  • Therefore, the size of string must be equal to the maximum number of characters+1 .

Example:

1. Program store name into string and display.
#include<stdio.h>
int main(){
char name[15] = "prowessapps.in";
 printf("%s",name);
  return 0;
}
OUTPUT:
prowessapps.in
2. Program to display a string entered by user..
#include<stdio.h>
int main() {
char s[50];
printf("Enter a string:");
scanf("%s",s);
printf("**************\n");
printf("Entered string is: %s\n",s);
printf("**************");
return 0;
}
OUTPUT:
FIRST RUN:

Enter a string:prowessapps.in
**************
Entered string is:
prowessapps.in
**************
-------------------------------------
SECOND RUN:

Enter a string:C is a programming language
**************
Entered string is:
C
**************

As you can notice that, in the second run only "C" is displayed instead of "C is a programming language".

This is because the scanf( ) in 'C' considers a space " " is a string terminating character.


Example:

C String to read a line of text
3. Program to read and display an entire line entered by user..
#include<stdio.h>
int main() {
char s[50];
printf("Enter a string:");
gets(s);
printf("**************\n");
printf("Entered string is: %s\n",s);
printf("**************");
return 0;
}
OUTPUT:
Enter a string:C is a programming language
**************
Entered string is:
C is a programming language
**************

To read the string containing blank space, use gets() function.

String Handling Functions:

C library supports a wide range of string handling functions, <string.h> is a standard header file that is use to accessing the string handling functions.

S.NoFunction NamePurpose
1.strcpy(str1, str2)The strcpy function copies string2 into string1.
2.strcat(str1, str2)The strcat function joins two string together.It copies string2 to string1.
3.strlen(str1)Returns the length of string str1.
4.strcmp(str1, str2)Returns 0 if str1 and str2 are the same; less than 0 if str1<s2; greater than 0 if s1>s2.
5.strstr(str1, str2)Returns a pointer to the first occurrence of string2 in string s1.

Example:

3. Program to makes use of the above-mentioned functions:.
#include<stdio.h>
#include<string>
int main() {
char str1[10]="Hello";
char str2[10]=" World";
char str3[20];

strcpy(str3,str1);
//str3 will be " Hello"
printf("strcpy(str3,str1) = %s",str3);

int l = strlen(str1);
printf("\nstrlen(str1) = %d",l);

strcat(str1,str2);
//str1 will be "Hello World"
printf("\nstrcat(str1,str2) = %s",str1);

int c = strcmp(str1,str2);
//c will be 1(one)
printf("\nstrcmp(str1,str2) = %d",c);

return 0;
}
OUTPUT:
strcpy(str3,str1) = Hello
strlen(str1) = 5
strcat(str1,str2) = Hello World
strcmp(str1,str2) = 1

Next topic is Structure





 







Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in


CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved