String functions of C program

String positions of char in C Strings are commonly used data types in C . Strings are also known as Character array as they consists of group of chars.
char name[]={'p','l','u','s','2','n','e','t','\0'};

Declaring a string in C

First position of the string starts with 0. While declaring a string we can specify the number of chars to be used or reserved in memory.

Null char at the end

When we declare a string, C adds one null character ( \0 ) at the end. So we can check for this null character to identify the end of the string.
char name[10]="plus2net";
We can insert one null char ( \0 ) to terminate the string.
char name[10]="plus2net";
name[3]='\0';
printf("%s",name);  // plu
Output
plu
We can check for this null character to identify the end of the string.
char name[]="plus2net";
int i=0;
while(name[i]!='\0'){
printf("%c ",name[i]);
i=i+1;
}
Output
p l u s 2 n e t
strcat()Append string
strncat()Append part of the string
strcpy()Copy string
strncpy()Copy part of a string
strlen() Length of a string
strrev() String Reverse
strlwr() Upper to lowercase letter
strupr() Lower to Uppercase letter
strchr() Searching for first occurrence of chracter
strrchr() Searching for last occurrence of chracter
strcmp() Case sensitive string comparison
strcmpi() Case insensitive string comparison
strstr() Searching for string
strset() Setting all chars of the string
strnset() Setting part of the chars of the string
strdup() Duplicating string
strtok() Parses a string into sequence of tokens
Basic string assignments

Sample Codes

Reading and displaying a string.
#include <stdio.h>
int main(void){
char my_name[20];
printf("Enter your name : ");
scanf("%s",my_name);
printf("Welcome %s",my_name);
return 0;
}
Printing string vertically with total number of chars
#include <stdio.h>
int main(void){
int i;
char str[]="Welcome";

for ( i=0;str[i] != '\0';i++){
printf( "%c \n",str[i]);
}
printf("\n Total number of chars = %d",i);
return 0;
}
W
e
l
c
o
m
e

 Total number of chars = 7
C Tutorials


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer