Tutorial references

c
c++
c#

Tutorial references

chapters

String

String

String is a collection of symbols.

String is a set of symbols.

String is also called set of characters.

We can use alphabets,digits and special characters in string.

   Alphabets:- A, B, ….., Y, Z a, b, ……, y, z

   Digits - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

   Special symbols - ~ ‘ ! @ # % ^ & * ( ) _ - + = | \ { } [ ] : ; " ' < > , . ? /

Syntax:

char variableName[ ] ={'symbol-1', 'symbol-2',........,'\0'};

OR

char variableName[size] ={'symbol-1', 'symbol-2',........,'\0'};

Example:

char str[ ] = {'s', 'a', 'b', 'b', 'l', 'a', '.', 'c', 'o', 'm', '\0'};

OR

char str[50] = {'s', 'a', 'b', 'b', 'l', 'a', '.', 'c', 'o', 'm', '\0'};

\0 represents NULL.

"string.h" header file include in your program.

%s placeholder is used to print value of string.

Example

#include <stdio.h>
#include <string.h>
#include <conio.h>

void main(){
 char str[]={'s','a','b','b','l','a','.','c','o','m','\0'};
 clrscr();
  printf("string value is: %s",str);
 getch();
}

Run program

Example

#include <stdio.h>
#include <string.h>
#include <conio.h>

void main(){
 char str[50]={'s','a','b','b','l','a','.','c','o','m','\0'};
 clrscr();
  printf("string value is: %s",str);
 getch();
}

Run program

We can print each and every character of string by loop.

Example

#include <stdio.h>
#include <string.h>
#include <conio.h>

void main(){
int i;
 char str[50]={'s','a','b','b','l','a','.','c','o','m','\0'};
 clrscr();
 for(i=0;i<=9;i++){
  printf("\n str[%d] = %c",i,str[i]);
 }
 getch();
}

Run program

String variable declaration

If a value is given in double quotes ("") it is called string.

Syntax:

char variableName[size] = value;

OR

char variableName[size];

Example:

char str[size] = "HELLO WORLD";

OR

char str[size];

Example

#include <stdio.h>
#include <string.h>
#include <conio.h>

void main(){
 char str[50]="HELLO WORLD";
 clrscr();
  printf("string value is: %s",str);
 getch();
}

Run program

gets()

gets() is a predefine function. It is used to read string value from user.

Example

#include <stdio.h>
#include <string.h>
#include <conio.h>

void main(){
 char str[50];
 clrscr();
  printf("What is c? => ");
  gets(str);
  printf("\nAnswer: %s",str);
 getch();
}

Run program

puts()

puts() function is predefine function. It is used to print value of string.

Example

#include <stdio.h>
#include <string.h>
#include <conio.h>

void main(){
 char str[50];
 clrscr();
  puts("What is c? => ");
  gets(str);
  puts(str);
 getch();
}

Run program

strlen()

It is predefine function. It returns length of string.

Example

#include <stdio.h>
#include <string.h>
#include <conio.h>

void main(){
 char str[50];
 int length;
 clrscr();
  printf("write some thing:-");
  gets(str);
  length = strlen(str);
  printf("\nlength of string: %d",length);
 getch();
}

Run program

strcpy()

It is a predefine function.

Syntax:

strcpy(destination, source);

It is used to copy from source variable and paste in destination variable.

In below example str1 is source and str2 is destination.

Example

#include <stdio.h>
#include <string.h>
#include <conio.h>

void main(){
 char str1[50];
 char str2[50];
 clrscr();
  printf("write some thing:-");
  gets(str1);
  strcpy(str2,str1);
  puts(str2);
 getch();
}

Run program

strcat()

It is used to concatenate strings.

Syntax:

strcat(string_1,string_2);

string_1 holds result.

Example

#include <stdio.h>
#include <string.h>
#include <conio.h>

void main(){
 char str1[50];
 char str2[50];
 clrscr();
  printf("Enter first string:-");
  gets(str1);
  printf("\nEnter second string:-");
  gets(str2);
  strcat(str1,str2);
  puts(str1);
 getch();
}

Run program

strcmp()

It is a predefine function. It check two string is equal or not. If both strings are equal then it returns 0.

Syntax:

strcmp(string_1,string_2);

Example

#include <stdio.h>
#include <string.h>
#include <conio.h>

void main(){
 char str1[50];
 char str2[50];
 int con;
 clrscr();
  printf("Eenter first string:-");
  gets(str1);
  printf("\nEenter second string:-");
  gets(str2);
  con = strcmp(str1,str2);
  if(con==0){
  printf("\n both are equal.");
  }
  else{
  printf("\n both are equal.");
  }
 getch();
}

Run program

strrev()

It prints reverse of string.

Example

#include <stdio.h>
#include <string.h>
#include <conio.h>

void main(){
 char str1[50];
 clrscr();
  printf("Eenter string:-");
  gets(str1);
  puts(strrev(str1));
 getch();
}

Run program

strlwr()

It converts uppercase string into lowercase.

Example

#include <stdio.h>
#include <string.h>
#include <conio.h>

void main(){
 char str1[50];
 clrscr();
  printf("Eenter string:-");
  gets(str1);
  puts(strlwr(str1));
 getch();
}

Run program

strupr()

It converts lowercase string into uppercase.

Example

#include <stdio.h>
#include <string.h>
#include <conio.h>

void main(){
 char str1[50];
 clrscr();
  printf("Eenter string:-");
  gets(str1);
  puts(strupr(str1));
 getch();
}

Run program

About Us

This website(www.sabbla.com) is very useful for beginners and professional developers. Sabbla provides tutorial in simple language. It provides latest technology tutorial which very useful for developers.

See More...

Contact Us

+91 9971366710

+91 9717983128

info@sabbla.com

Buxar, Bihar, India, Pin No.- 802131

Services

Sabbla is providing free latest technology tutorials. All tutorials are free for beginner and professionals.


Terms of Use

sabbla.com is not responsible for any mistake. Users can inform to sabbla.com for any mistake. But sabbla.com always try for 0-0 mistake.

Privacy policy

Currently sabbla.com does not collect any data from users.

                   

copyright © 2017-2018