Introduction to C Programming - Strings and Functions

Use of puts and gets in C for reading and printing a string

 
      #include<stdio.h>
      #include<string.h>
        int main()
        {
          int i = 0;
          char name[20] = "";
          printf("Enter the name");
          gets(name);
          puts(name);
          system("pause");
          return 0;
        }
    
Output

Reading a char and displaying a character

 
      #include<stdio.h>
  #include<string.h>
    int main()
    {
      int i = 0;
      char name[20] = "";
      char inputchar=' ';
      printf("Enter a character");
      inputchar=getchar();
      putchar(inputchar);
      system("pause");
      return 0;
    }
    
Output

Reading a character using getchar and display using putchar

 
      #include<stdio.h>
    int main()
    {
      int i = 0;
      char name[7] = "";
      while ((name[i]=getchar()) != '\n')
      {
        i++;
      }
      i = 0;
      while (name[i] != '\0')
      {
        putchar(name[i]);
        i++;
      }
      system("pause");
      return 0;
    }
    
Output

Finding the length of a String

 
      #include<stdio.h>
      int main()
      {
        char a[7] = "satish";
        printf("%d", strlen(a));
        system("pause");
        return 0;
      }
    
Output

Concatenation of Two Strings in C

 
      #include<string.h>
      int main()
      {
        int i = 0;
        char name[20] = "";
        char finalname[30] = "Mr.";
        printf("Enter your name");
        gets(name);
        strcat(finalname, name);
        puts(finalname);
        system("pause");
        return 0;
      }
    
Output

strcpy function in C

 
      #include<string.h>
      int main()
      {
        int i = 0;
        char name[20] = "Satish";
        char finalname[30] = "";
        strcpy(finalname, name);
        puts(finalname);
        system("pause");
        return 0;
      }
    
Output

strcmp function in C

 
      #include<string.h>
    int main()
    {
      int i = 0;
      int result;
      char name[20] = "Satish";
      char finalname[30] = "Satish";
      result=strcmp(finalname, name);
      printf("%d", result);
      system("pause");
      return 0;
    }
    
Output

Sorting a list of names in ascending order in C

 
      #include<stdio.h>
  #include<string.h>
    int main()
    {
      char name[4][20] = {""};
      char temp[20]="";
      int i = 0,j=0,n=4;
      printf("Enter the names");
        for(i = 0; i < 4; i++)
        {
          gets(name[i]);
        }
        for (i = 0; i < n - 1; i++)
        {
          for (j = 0; j < n - 1 - i; j++)
          {
            if (strcmp(name[j], name[j + 1])>0)
            {
              strcpy(temp, name[j]);
              strcpy(name[j], name[j + 1]);
              strcpy(name[j + 1], temp);
            }
          }
        }
        
        for (i = 0; i < 4; i++)
        {
          puts(name[i]);
        }
      system("pause");
      return 0;
    }
    
Output

Searching for a name in a array of names

 
      #include<stdio.h>
      #include<string.h>
        int main()
        {
          char name[4][20] = {""};
          char temp[20]="";
          int i = 0,n=4,found=0;
          printf("Enter the names");
            for(i = 0; i < 4; i++)
            {
              gets(name[i]);
            }
            printf("Enter the name you wish to search");
            gets(temp);
            for (i = 0; i < n; i++)
            {
              if (strcmp(name[i], temp) == 0)
              {
                printf("The name is found");
                found = 1;
                break;
              }
            }
            
            if (found == 0)
            {
              printf("Name is not found");
            }
          system("pause");
          return 0;
        }
    
Output

Check if the student is from BCE or BEC branch using registration number

 
      #include<stdio.h>
      #include<string.h>
        int main()
        {
          char name[20] = {""};
          char temp[20]="";
          int i = 0,j=0;
          printf("Enter the names");
          gets(name);
          for (i = 0; i < strlen(name); i++)
            {
              if (i >= 2 && i<5)
              {
                temp[j] = name[i];
                j++;
              }
            }
          if (strcmp("BCE", temp) == 0)
          {
            printf("you are from computer science");
          }
          else if (strcmp("BEC", temp) == 0)
          {
            printf("you are from Electronics");
          }
          else
          {
            printf("Invalid reg number");
          }
            
          system("pause");
          return 0;
        } 
    
Output