C STRINGS

  • It is an array/sequence of characters that ends with null characters ().
  • Null characters represent the end of the string.
  • String in c is always enclosed within double quotes (” “)
  • Character in c is enclosed within a single quote (‘,)
SYNTAX:- char str_name[Size];

Initializing of String

char str[10]="HELLO WORLD";
char str[]="HELLO WORLD";
char str[10]={'A''B'C'D','10}';
char str[ ]={'A''B'C'D','10}'

Using scanf for a string with white space without termination

EXAMPLE: scanf("%"[n]s",st1); 
  • Here [n] indicates that scanf() will keep receiving characters in “st1[]” until ‘n’ is encountered  
  • Through workable, but this is not the best of the ways to call a function.
Using gets() and puts():
main()
{
    char name[20];
    printf("Enter your full name:");
    gets(name);
    puts(name);
}
Output: Enter your full name:
admin
admin:

Standard libraray string function

FUNCTION

strlen

strlwr

strupr

strcat

strncat

strepy

strncpy

strcmp

strcmpi

strnicmp

strdup

Strchr

Strrchr

Strstr

Strset

Strnset

Strrev

USE

Finds the length of a string

Converts a string to lowercase

converts a string to uppercase

Append one string at the end of another

Append the first n characters of a string at the end of another

Copy a string into another 

copy the first n characters of one string into another 

compares two strings 

compares first n characters of two strings 

compares two strings without regard to the case (identical to strempi)

compares the first n character of two strings without regard to the case

FInds first  occurrence of a given character in a string

FInds last  occurrence of a given character in a string

FInds first  occurrence of a given string in another string

sets all characters of a string to a given character 

set first n character of a string to a given character

Reverses string

 

  • Strlen()
  • The return type of this function is int 
  • It returns the length of the string and the number of characters present in the string 
  • It accepts a single argument which is pointed to the first character of the string.
Syntax():- Strlen(char const str);
# Write a program to accept any string and count its length
#include<stdio.h>
#include<conio.h>
#include<String.>
void main()
{
    char Str[50];
    printf("Enter any string:");
    gets(Str);
    l=Strlen(str);
    printf("The length of string is %d",l);
    getch();
}
  • Strcmp()
  • The return type of this function is int
  • This function compares two strings if the two strings match it returns a value ‘0’ else returns a non-zero value
  • This function compares the strings character by character and the compassion when either the end of the string is reached 
Syntax:int strcmp (const char* str1, const char* str2);
#include<conio.h>
#include <stdio.h>
#include <string.h>
int main() {
  char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd";
  int result;
  result = strcmp(str1, str2);
  printf("strcmp(str1, str2) = %dn", result);
  result = strcmp(str1, str3);
  printf("strcmp(str1, str3) = %dn", result);
  return 0;
}
  • Strcpy():
  • It is used for copying one string into another string
  • it copies String 2 into String 1 including the nun character
Syntax:char* strcpy(char* destination, const char* source);
Example:void main()
{
    cahr a[35],b[50];
    printf("Enter the string");
    scanf(%s[/n]s",a);
    printf("The copied string is %s",b);
    getch();
}

 

  • Strcat():
  • This function is used for the concatenation of two strings which means it is used to append the content of one string at the end of another string.
  •  While appealing the null character will be removed from one string and added at the end of another string.
  • A pointer to the first string is returned by the function 
Syntax Strcat(char String,char const String);
//Write a program to concat two string
#include<stdio.h>
#include<conio.h>
void main()
{
    char a[50],b[20];
    printf("Enter any two string");
    gets(a);
    gets(b);
    Strcat("%s",a);
    getch();
}
  • Strncat();
  • This function appeals first n character of a string at the end of another String.
  • That means it concatenates n characters of string two into string one.
Syntax:- Strcat(char*string1,char const string2,int n);
//Write a program to concat 4 character of string 2 into String 1.
include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
    char Str[30]="Hello";
    char Str[30]="World";
    Strn cat(str,st,4)
    printf("%s"),str);
    getch();
}
Strchr()
  • It searches & fetches out characters from a string.

 

SYNTAX:-Strchr(char string,int)

gets:-to insert /accept any string

puts:- to point any string

void main()
{
    char S1[30];
    puts("Enter any string");
    gets(s1);
    puts(strchr (S1,^e');
    getch();
}

Strstr()

Finds 1st occurrence of given substring into another String.

SYNTAX:-Strstr(char*String1,Char)

EXAMPLE

Void main()
{
char S1[30]="Hello WORLD";
printf("%s",Str (S1,"hello"));
Strstr(S1,"Hello");
printf("%s",S1);
getch();
}