C Programs on Strings

C Program to count the number of words and characters in a line

Code:

#include<stdio.h>
#include<string.h>
int main()
{
    char line[50], ctr;
    int i,c;
    int end=0,characters=0,words=0;
    printf("Enter a text: ");
    while (end==0)
    {
        c=0;
        while ((ctr=getchar()) != '\n')
        {
            line[c++] = ctr;
            line[c] = '\0';
        }
        if (line[0] == '\0')
        {
            break;
        }
        else
        {
            words++;
            for ( i = 0; line[i] != '\0'; i++)
            {
                if (line[i] == ' ' || line[i] == '\t')
                {
                    words++;
                }
            }
        }
        characters = characters + strlen(line);
        break;
    }
    printf("\n");
    printf("Number of words: %d\n",words);
    printf("Number of characters: %d\n",characters);
    return 0;
}

Output:

Enter a text: Welcome to Codedec!
Number of words: 3
Number of characters: 19

C Program to reverse a String

Code:

#include<stdio.h>
#include<string.h>
 
void Rev(char*);
 
int main()
{
    char s1[100];
 
    printf("Enter a String: ");
    gets(s1);
    Rev(s1);
    printf("Reverse order of the entered String is \"%s\".\n",s1);
    return 0;
}
 
void Rev(char *s1)
{
    int i,j;
    char *start, *end, temp;
    j = strlen(s1);
    start = s1;
    end = s1;
 
    for (i = 0; i < j - 1; i++)
        end++;
 
    for (i = 0; i < j/2; i++) { 
        temp = *end;
        *end = *start;
        *start = temp;
 
        start++;
        end--;
    }
}

Output:

Enter a String: CODEDEC
Reverse order of the entered String is "CEDEDOC".

C Program to compare two Strings

Code:

#include<stdio.h>
int main()
{
    int i=0;
    char s1[8] = "CODEDEC";
    char s2[8] = "CODEDEC";
    while (s1[i] == s2[i] && s1[i] != '\0' && s2[i] != '\0')
    {
        i = i+1;
    }
    if (s1[i] == '\0' && s2[i] == '\0')
    {
        printf("Equal\n");
    }
    else
    {
        printf("Not Equal\n");
    }
    return 0;
}

Output:

Equal

C Program to Find the Length of a String

Code:

#include <stdio.h>
int main()
{
    char array[] = "Welcome to Codedec";
    int i;
    for (i = 0; array[i] != '\0'; ++i);
    printf("Length of the string: %d\n", i);
    return 0;
}

Output:

Length of the string: 18

C Program to concatenate multiple Strings

Code:

#include<stdio.h>
int main()
{
  int i,j;
  char s1[10] = {"WELCOME "};
  char s2[10] = {"TO "};
  char s3[10] = {"CODEDEC"};
  char result=0;
  
  while (s1[result] != '\0')
  {
    ++result;
  }
  for ( i = 0; s2[i] != '\0'; ++i, ++result)
  {
    s1[result] = s2[i];
  }
  for ( j = 0; s3[j] != '\0'; ++j, ++result)
  {
    s1[result] = s3[j];
  }
  s1[result] = '\0';
  puts(s1);
  return 0;
}

Output:

WELCOME TO CODEDEC

C Program to Copy String Without Using strcpy()

Code:

#include <stdio.h>
int main()
{
    char string1[100], string2[100], i;
    printf("Enter first string: ");
    fgets(string1, sizeof(string1), stdin);
    for (i = 0; string1[i] != '\0'; ++i) {
        string2[i] = string1[i];
    }
    string2[i] = '\0';
    printf("Second string is %s", string2);
    return 0;
}

Output:

Enter first string: CODEDEC
Second string is CODEDEC

C Program to Sort Elements in Lexicographical Order (Dictionary Order)

Code:

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

int main()
{
   char string[10][50], array[50];
   printf("Enter 3 words: ");
   for (int i = 0; i < 10; ++i)
   {
      fgets(string[i], sizeof(string[i]), stdin);
   }
   for (int i = 0; i < 10; ++i)
   {
      for (int j = i + 1; j < 10; ++j)
      {

         if (strcmp(string[i], string[j]) > 0)
         {
            strcpy(array, string[i]);
            strcpy(string[i], string[j]);
            strcpy(string[j], array);
         }
      }
   }

   printf("Lexicographical order is:\n");
   for (int i = 0; i < 10; ++i)
   {
      fputs(string[i], stdout);
   }
   return 0;
}

Output:

Enter 3 words: Book
Zebra
Car
Dark
Light
Matter
Wave
Device
Program
Learn
Lexicographical order is:
Book
Car
Dark
Device
Learn
Light
Matter
Program
Wave
Zebra