STRING IN C | My CS Tutorial - My CS Tutorial

Breaking

Programming languages full tutorial and programs, exam papers, visual basic( Vb.net ), information of new technologies and more....

Tuesday, May 26, 2020

STRING IN C | My CS Tutorial

WHAT IS STRING IN C | STRING IN C | C STRING | C - STRING AND STRING FUNCTION | INPUT AND OUTPUT FUNCTIONS | INITIALIZE,DECLARE,OPEN,READ AND PRINT WITH EXAMPLE | My CS Tutorial



String in C language | My CS Tutorial

String


 String is nothing but the collection of the individual array elements or characters
stored at contiguous memory locations

 String is enclosed within Double quotes.

 String always Terminated with NULL Character (‘/0′).

Syntax:
Char Stringname[sizeofcharacters];

Example : char name[10];

Initializing String [Character Array] :

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

char name [] = {'P','R','I','T','E','S','H','\0'};

In the above example name is assigned with a string PRITESH and Ends with Null
Character.

Program example for basic string concept
Void main()
{
Char name[10];
Printf(“Enter your name:-\n”);
Scanf(“%s”,name);
Printf(“The entered name is %s”, name);
}
Output:
Enter your name:-Pradhan
The entered name is Pradhan

WHAT IS STRING IN C | STRING IN C | C STRING | C - STRING AND STRING FUNCTION | INITIALIZE,DECLARE,OPEN,READ AND PRINT WITH EXAMPLE


Inbuilt String input and Output function.


  gets() 

Reads characters from the standard input (stdin) and stores them as a C
string
Syntax:
gets( <variable-name> )

  puts()

puts() can be used to display message.
puts(string_Variable_name) ;
#include<stdio.h>
void main()
{
char name[20];
printf("\nEnter the Name : ");
gets(name);
puts(name);
}

In above example gets(name) is used to get the input from user and
puts(name) is to print the name readed from user.

  getchar()

getchar() function is also one of the function which is used to accept the
single character from the user.
Syntax for Accepting String and Working :
/* getchar accepts character & stores in ch */
char ch = getchar();

  putchar()

putchar() displays character stored in variable.

Syntax
putchar(Variable);
#include<stdio.h>
void main()
{
char c;
printf("\nEnter a character ");
c=getchar();
putchar(c);
}
Inbuilt String Functions in C

  strlen() : Finding Length of String

void main()
{
 Char name=”Hello”;
 Int len=strlen(name);
 Printf(“The length of the String is %d”,len);

}
Output: 5

  Strcat()
It is used to combine two strings together.
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "This is ", str2[] = "crossmove";
//concatenates str1 and str2 and resultant string is stored in str1.
strcat(str1,str2);
puts(str1);
puts(str2);
return 0;
}

Output
This is crossmove
Crossmove

  strcpy()

The strcpy() function copies the string pointed by source (including the null
character) to the character array destination.
#include <stdio.h>
#include <string.h>
int main()
{
 char str1[10]= "awesome";
 char str2[10];
 char str3[10];
 strcpy(str2, str1);
 strcpy(str3, "well");
 puts(str2);
 puts(str3);
 return 0;
}
Output : awesome
Well

  strcmp()

The strcmp() compares two strings character by character. If the first character
of two strings are equal, next character of two strings are compared. This
continues until the corresponding characters of two strings are different or a
null character '\0' is reached.
#include <stdio.h>
#include <string.h>
int main()
{
 char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd";
int result;
 // comparing strings str1 and str2
 result = strcmp(str1, str2);
 printf("strcmp(str1, str2) = %d\n", result);
 // comparing strings str1 and str3
 result = strcmp(str1, str3);
 printf("strcmp(str1, str3) = %d\n", result);
 return 0;
}
Output
strcmp(str1, str2) = 32
strcmp(str1, str3) = 0
The first unmatched character between string str1 and
str2 is third character. The ASCII value of 'c' is 99 and the ASCII value of 'C'
is 67. Hence, when strings str1 and str2 are compared, the return value is
32.
When strings str1 and str3 are compared, the result is 0 because
both strings are identical.

  strlwr( ) 

strlwr( ) function converts a given string into lowercase.
#include<stdio.h>
#include<string.h>
int main()
{
 char str[ ] = "MODIFY ";
 printf("%s\n",strlwr (str));
 return 0;
}
OUTPUT:
modify

  strrev()

strrev( ) function reverses a given string in C language.
#include<stdio.h>
#include<string.h>
int main()
{
 char name[30] = "Hello";
printf("String before strrev( ) : %s\n",name);
 printf("String after strrev( ) : %s",strrev(name));
 return 0;
}
OUTPUT:
String before strrev( ) : Hello
String after strrev( ) : olleH

  strupr()

strupr( ) function converts a given string into uppercase.
#include<stdio.h>
#include<string.h>
int main()
{
 char str[ ] = "Modify This String To Upper";
 printf("%s\n",strupr(str));
 return 0;
}
Output : MODIFY THIS STRING TO UPPER


WHAT IS STRING IN C | STRING IN C | C STRING | C - STRING AND STRING FUNCTION | INITIALIZE,DECLARE,OPEN,READ AND PRINT WITH EXAMPLE



Please share this post and blog link with your friends.For more programs use this blog.
If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this program or want to give any suggestion send email on hc78326@gmail.com

Created by-- HARSH CHAUHAN


No comments:

Post a Comment