strings in c programming language

Strings In C Programming Language

Social Shares
  • 2
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Hi Guys, welcome to our blog. In the last article, you had learnt about the array in c programming language. I hope you all understand how to create an array, how to initialise an array but in today’s article, you will learn how to declare strings in c programming language.  It is also a part of the array.  So let’s get started.

Declare Strings In C Programming Language

There is no data type available to create strings in the C programming language. You need to create char arrays to store a string in C language. Therefore, in C, the character array is also called string. In the C programming language, the string is terminated from the null character (\0).

In C you can declare and initialize strings in c programming language in 2 ways. Īn the first method, you create an array of a finite number and in each of its index, there is a character store of the string. The null character (\0) is stored in the last index of this array. It is necessary to do this. An example of this is being given below.

char myArray[4] = {B,H,T,\0};
In a second way, you create an undefined char array and assign it a string. Array automatically gets the same size as the string size. In such an initialization the null character (\0) gets automatically added. This method is an easy way to create strings in c programming language, so it is used more. An example of this is being given below.
char myArray[] = “BHT”;
When you declare a string in this manner, it can never be changed. Below is a complete example of how to use strings in c programming language.
#include <stdio.h>
int main()

{

   char myArrayF[4] = {‘B’,’H’,’T’,’\0′};
   char myArrayS[] = “BHT”;
   printf(“First Way : %s\n”,myArrayF);

   printf(“Second Way : %s”, myArrayS);   return 0;

}
The above program produces the below-given output.
First Way: BHT
Second Way: BHT

 

gets() and puts() Functions

If you want to read from the string used at runtime, you can use the get() function for it. This function has been defined for the same purpose. In this function, you pass the name of the char array in which you want to store the string. The general structure of this function is given below.
gets(char_Array_Name);
Whatever string you pass is stored in the given char array. Now if you print that string from this array then you have to use the loop. But you do not need to do this. C language provides puts () function to print a char array as a complete string. In this function, those char arrays are passed which you want to print as a complete strings in c programming language. Its general structure is given below.
puts(char_Array_Name);
Both of these functions are explained below with the use complete example.
#include <stdio.h>
#include <conio.h>
int main()
{
    char answer[50];
    printf(“What is CK?”);
    gets(answer);
    printf(“\nAnswer is : “);

    puts(answer);    return 0;

}

The above program produces the below-given output.

What is CK?
Coding Kida                              (input)
The answer is: Coding Kida

Other String Functions

The C language provides you with more functions to perform operations with strings in c programming language. All these functions are being given below.

strlen()

These are used to calculate the length of the function string. In this function, you pass the name of the char array in which string is stored. Its syntax is given below.
strlen(char_Array_Name); //will return the length of string

strcpy()

This function is used to copy the string from another array into another array. The first argument of this function is that it is a char array in which you want to copy the string and the second argument is the char array, so that you want to copy the string. The syntax of this function is given below.
strcpy(destinationCharArray, sourceCharArray);

strcat()

This function combines two strings (concatenate). Two arguments are passed in this function. These two arguments are the only character array. When this function is called call, then both strings combine and the result is stored in another char array.
strcat(charArray1,charArray2);  //combine both strings and store result in first one.

youtubeembedcode.com
Top 10 Geeks


Social Shares
  • 2
  •  
  •  
  •  
  •  
  •  
  •  
  •  

5 comments

  1. It’s far very useful and right articles and i love this article. We’re able to effortlessly recognize this article due to the fact the author did no longer use tough and horrible languages and without a doubt specifies all of the information approximately the concern. Some articles have now not following the suitable writing format however right here i got new way of writing. I like the way of writing and the offering. Maintain sharing’s can percent this newsletter for my friends and colleagues. Brilliant post. Thanking you for this put up shared with us. Its miles a form of evaluation and i wish it’ll be more beneficial to the readers

Leave a Reply

Your email address will not be published. Required fields are marked *