ankitweblogic logo
W e b t u t o r i a l s

C Tutorial

Programing Approach

Algorithm

Flow Chart

Define C

Define Programming

C - Syntax

Compiler vs Interpreter

C - Variables

C - Keywords

C - Data Types

C - Comments

Turbo C - Shortcut keys

C - Operators

Hierarchy of Operators

Ex: Arithmetic Operator

C - Formatting Output

Constant Variable

C - Escape Sequence

C - if statement

Ex: If statement

C - switch statement

Ex: Inc / Dec Operator

C - loops

Ex: Loops

C - Nesting Of loops

Ex: Nested Loops

Jumping Statements

C - Arrays 1D

C - Arrays 2D

C - Sorting

ASCII Value

Character I/O Function

C - String Functions

Ex: Strings

Array of Strings

C - Math Functions

User-defined Function

Exercise Function

C - Types of Functions

Storage Class

C - Header Files

C - Preprocessor

C - Pointers

C - Structures

C - Typedef

C - File Handling

Ex: File Handling

Command Line Argument

MCQ

C String Functions

String is a collection of characters or String is an array of character which is terminated by a null character '\0'.

String input functions in 'stdio.h' file

To input a string use format '%s' eg. scanf("%s",&variable-name); limiation of using scanf() is that it will not accept space while inputing a string. To accept sentance including spaces use following functions:

scanf("%[^\n]s", variable-name);

gets() - get means input, s means string/sentance it is use to input a string including spaces.
Syntax: gets(variable-name);

String functions in string.h file.

S.No Function Syntax Description
1. strlen l=strlen(a); Calculate length of String and store in integer variable.
2. strrev strrev(a); Reverse the string.
3. strupr strupr(a); Convert string into capital letters.
4. strlwr strlwr(a); Convert string into smaller letters.
5. strcpy strcpy(b,a); Copy contents of a into b.
6. strcat strcat(b,a); Append contents of one string into another.
7. strcmp l=strcmp(a,b); Compare two strings and return a integer value. Value is one of the following:
a. Less than 0 if b is greater
b. Greater than 0 if a is greater
c. Equal to 0 if both are equal
8. strcmpi or stricmp len=strcmpi(a,b); Compare 2 strings same as strcmp but it will ignore cases.
9. strncmp strncmp(b,a,n); Compare two Strings but only 1st n specified characters
10. strncmpi strncmpi(b,a,n); Compare two Strings but only 1st n specified characters and ignore cases.
11. strncpy strncpy(b,a,n); Copy only n specified characters, where n is a integer number.
12. strncat strncat(b,a,n); Concatenate only n specified characters.

Exercise questions on String:

  1. Input string and find its length.
  2. #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    
    int main()
    {
        char name[20];
        int len;
        clrscr();
        printf("Enter name");
        gets(name);
        len = strlen(name);
        printf("Length = %d",len);
        getch();
    }
  3. Input string and reverse it.
  4. #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    
    int main()
    {
        char s[20];
        clrscr();
        printf("Enter String");
        gets(s);
        strrev(s);
        printf("Reverse String is %s", s);
        getch();
    }
  5. Input string and convert it into capital letters.
  6. #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    
    int main()
    {
        char s[30];
        clrscr();
        printf("Enter string");
        gets(s);
        strupr(s);
        printf("String in Capital = %s",s);
        getch();
    }
  7. Input string and convert it into small letters.
  8. Input string and copy into another variable.
  9. #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    
    void main()
    {
       char s1[20], s2[20];
       clrscr();
       printf("Enter string");
       gets(s1); //input string
       strcpy(s2,s1); //copy string
       printf("After Copy String is: %s",s2); //output new string
       getch();
    }
  10. Input string and concatenate it.
  11. Input 2 strings and find both strings are equal or not, if not, which string is greater.
  12. Compare 2 strings but ignore cases.
  13. Compare 2 strings but only n specified characters.