character array and string in c programming

character array and string in c...


  • string is a sequence of characters that is uses as a single data item and terminated by null character '\0'. Remember that C language does not support strings as a data type.
  • A string is  1-D array of characters in C language. These are often used to create meaningful and readable programs.

         For example : The string "hello world" contains 12 characters including '\0' character which is            automatically added by the compiler at the end of the string.

         Initializing String [Character Array] :

          When we declare a String then it will contain garbage values inside it. We have to initialize                 String  or Character array before using it. Process of Assigning some legal default data to                    String is Called Initialization of String. There are different ways of initializing String in C                    Programming –

          Initializing Unsized Array of Character
          Initializing  String Directly
          Initializing  String Using Character Pointer

1 : Unsized Array and Character


          Unsized Array : Array Length is not specified while initializing character array using this           approach


  • Array length is Automatically calculated by Compiler Individual Characters are written inside Single Quotes , Separated by comma to form a list of characters. Complete list is wrapped inside Pair of Curly braces
  •  Please Note : NULL Character should be written in the list because it is ending or terminating              character in the String/Character Array

           char name [] = {'h','e','l','l','o'\0'};

2 : Directly initialize String Variable



  • here ,this method we are directly assigning String to variable by writing text in double quotes.
  • In this type of initialization , we don’t need to put NULL or Ending / Terminating character at the end of string. It is appended automatically by the compiler.

          char name [ ] = "hello";

 3 : Character Pointer Variable



  • Declare Character variable of pointer type so that it can hold the base address of “String”
  • Base address means address of first array element i.e (address of name[0] )

        NULL Character is appended Automatically
        char *name = "hello";

String Input and Output...



  • Input function scanf() can be used with %s format specifier to read a string input from the terminal. But there is one problem with scanf() function, it terminates its input on first white space it encounters. 
  • Therefore if you try to read an input string "Hello World" using scanf() function, it will only read Hello and terminate after encountering white spaces.

            #include<stdio.h>
            #include<conio.h>
            #include<string.h>
         void main()
           {
          char str[20];
          clrscr();
           printf("Enter a string");
           scanf("%[^\n]",&str); //scanning the whole string,including the white spaces
           printf("%s",str);
           getch();

String Handling Functions...



  • C language supports a large number of string handling functions that can be used to carry out many of the string manipulations. These functions are packaged in string.h library. Hence, you must include string.h header file in your program to use these functions.


         The following are the most commonly used string handling functions.

         Method

  • strcat() It is used to concatenate(combine) two strings
  • strlen() It is used to show length of a string
  • strrev() It is used to show reverse of a string
  • strcpy() Copies one string into another
  • strcmp() It is used to compare two string
  • strcat() function


         strcat("hello","world");
         strcat() function will add the string "world" to "hello" i.e it will ouput helloworld.
         strlen() function

         strlen() function will return the length of the string passed to it.

         int j;
        strings.

        int j;
        j=strcmp("study","tonight");
     
        #include
       #include

      int main()
      {
          char s1[50];
          char s2[50];

          strcpy(s1, "StudyTonight"); //copies "studytonight" to string s1
          strcpy(s2, s1);  //copies string s1 to string s2

          printf("%s\n", s2);
         getch();
         return(0);
          }
       output:
       StudyTonight
       strrev() function

         It is used to reverse the given string expression.

          #include
          #include
          int main(){
         char s1[50];

        printf("Enter your string: ");
       gets(s1);
       printf("\nYour reverse string is: %s",strrev(s1));
       getch();
       return(0);
       }
      output:
      Enter your string: studytonight
      Your reverse string is: thginotyduts

No comments:

Post a Comment