C PROGRAM ON STRING

C program to Compare Two Strings

/*
 * C program to Compare Two Strings using loops.
 */
#include <stdio.h>
 
int main ()
{
         int count1 = 0, count2 = 0, flag = 0, i;
         char string1[30], string2[30];
 
         printf ("Enter the First string\n");
         gets (string1);
 
         printf ("Enter the Second string\n");
         gets (string2);
 
         while (string1[count1] != '\0')
                  count1 ++;
 
         while (string2[count2] != '\0')
                  count2 ++;
 
         i = 0;
 
         while (string1[i] == string2[i] && string1[i] != '\0')
         {
                  i ++;
         }
 
         if (string1[i] > string2[i])
                  printf ("First string is greater than Second string\n");
         else if (string1[i] < string2[i])
                  printf("Second string is greater than First string\n");
         else
                  printf ("Both strings are EQUAL\n");
 
         return 0;
}

 

Runtime Test Cases

Testcase 1: The first string entered here is “hello” and the second string is “hell”.

Enter the First string

hello

Enter the Second string

hell

First string is greater than Second string

Testcase 2: The first string entered here is “hello” and the second string is “helz”.

Enter the First string

hello

Enter the Second string

helz

Second string is greater than First string

C Program to Delete All Repeated Words in String

/*

 * C Program to Delete All Repeated Words in String

 */

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

 

int main ()

{

         char str[100], word[100], twoD[10][30];

         int i = 0, j = 0, k = 0, len1 = 0, len2 = 0, l = 0;

 

         printf ("Enter the string\n");

         gets (str);

 

         // let us convert the string into 2D array

         for (i = 0; str[i] != '\0'; i++)

         {

                  if (str[i] == ' ')

                  {

                            twoD[k][j] = '\0';

                            k ++;

                            j = 0;

                  }

                  else

                  {

                            twoD[k][j] = str[i];

                            j ++;

                  }

         }

 

         twoD[k][j] = '\0';

 

         j = 0;

         for (i = 0; i < k; i++)

         {

                  int present = 0;

                  for (l = 1; l < k + 1; l++)

                  {

                            if (twoD[l][j] == '\0' || l == i)

                            {

                                     continue;

                            }

 

                            if (strcmp (twoD[i], twoD[l]) == 0) {

                                     twoD[l][j] = '\0';

                                     present = present + 1;

                            }

                  }

                  // if (present > 0)              | uncomment this `if` block if you

                  // {                             | want to remove all the occurrences

                  //       twoD[i][j] = '\0';   | of the words including the word

                  // }                             | itself.

         }

 

         j = 0;

 

         for (i = 0; i < k + 1; i++)

         {

                  if (twoD[i][j] == '\0')

                            continue;

                  else

                            printf ("%s ", twoD[i]);

         }

 

         printf ("\n");

 

         return 0;

}

Enter the string

welcome to sanfoundry's c programming class ,  welcome again to c class !

welcome to sanfoundry's c programming class , again !

 

Enter the string:

Welcome to Sanfoundry C Class, Welcome to Java Programming, Welcome to C++ class

Welcome to Sanfoundry C Class, Java Programming, C++ class

C Program to Replace all Characters by Lowercase

/*
 * C Program to Replace all the Characters by Lowercase 
 */
#include <stdio.h>
#include <string.h>
 
int main()
{
   char string[1000];
 
   printf("Input a string to convert to lower case\n");
   gets(string);
 
   printf("Input string in lower case: %s\n",strlwr(string));
 
   return  0;
}

Runtime Test Cases

Input a string to convert to lower case

CHANDANA chanikya

rAVELLA

 

Input string in lower case:

chandana chanikya

ravella

C Program to Find the Frequency of a Substring in a String

/* 
 * C Program to Find the Frequency of Substring in 
 * the given String
 */
#include <stdio.h>
#include <string.h>
 
void main()
{
    int count = 0, i, j = 0, k;
    char str[100], str1[20];
 
    printf("Enter the string:\n");
    scanf(" %[^\n]s", str);
 
    printf("Enter the substring to be matched:\n");
    scanf(" %[^\n]s", str1);
 
    k = strlen(str1);
 
    for (i = 0; str[i] != '\0'; i++)
    {
        while (str[i] == str[j])
        {
            j ++;
        }
 
        if (j == k)
        {
            count ++;
            j = 0;
        }
    } 
    printf("No of matches of substring in main string is: %d\n", count);
}

Runtime Test Cases

Enter the string:

prrrogram is prrrogramming

Enter the substring to be matched:

rr

No of matches of substring in main string is: 4

 

Enter the string:

Sanfoundry C Programming

Enter the substring to be matched:

oun

No of matches of substring in main string is: 1

C Program to Count Number of Unique Words in a String

/*

 * C Program to Count the Number of Unique Words

 */

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

int main()

{

    int i = 0, e, j, d, k, space = 0;

    char a[50], b[15][20], c[15][20];

 

    printf("Read a string:\n");

    fflush(stdin);

    scanf("%[^\n]s", a);

    for (i = 0;a[i] != '\0';i++)        //loop to count no of words

    {

        if (a[i] =  = ' ')

            space++;

    }

    i = 0;

    for (j = 0;j<(space + 1);i++, j++)    //loop to store each word into an 2D array

    {

        k = 0;

        while (a[i] != '\0')

        {

            if (a[i] == ' ')

            {

                break;

            }

            else

            {

                b[j][k++] = a[i];

                i++;

            }

        }

        b[j][k] = '\0';

    }

    i = 0;

    strcpy(c[i], b[i]);

    for (e = 1;e <= j;e++)        //loop to check whether the string is already present in the 2D array or not

    {

        for (d = 0;d <= i;d++)

        {

            if (strcmp(c[i], b[e]) == 0)

                break;

            else

            {

                i++;

                strcpy(c[i], b[e]);

                break;

            }

        }

    }

    printf("\nNumber of unique words in %s are:%d", a, i);

    return 0;

}

$ cc string7.c

$ a.out

Read a string:

Welcome to Sanfoundry's C-programming class,  Welcome again to C class!

The length of input string is:70

 

Number of unique words in Welcome to Sanfoundry's C-programming class,  Welcome again to C class! are:8

C Program to Find All Possible Subsets of a String

/*

 * C Program to find the possible subsets of the String

 */

#include <stdio.h>

 

char string[50], n;

void subset(int, int, int);

 

int main()

{

    int i, len;

 

    printf("Enter the len of main set : ");

    scanf("%d", &len);

    printf("Enter the elements of main set : ");

    scanf("%s", string);

    n = len;

    printf("The subsets are :\n");

    for (i = 1;i <= n;i++)

        subset(0, 0, i);

}

 

/*Function to find the number of subsets in the given string*/

 

void subset(int start, int index, int num_sub)

{

    int i, j;

    if (index - start + 1  ==  num_sub)

    {

        if (num_sub  ==  1)

        {

            for (i = 0;i < n;i++)

                printf("%c\n", string[i]);

        }

        else

        {

            for (j = index;j < n;j++)

            {

                for (i = start;i < index;i++)

                    printf("%c", string[i]);

                printf("%c\n", string[j]);

            }

            if (start != n - num_sub)

                subset(start + 1, start + 1, num_sub);

        }

    }

    else

    {

        subset(start, index + 1, num_sub);

    }

}

$ cc string19.c

$ a.out

Enter the len of main set : 11

Enter the elements of main set : programming

The subsets are :

p

r

o

g

r

a

m

m

i

n

g

pr

po

pg

pr

pa

pm

pm

pi

pn

pg

ro

rg

rr

ra

rm

rm

ri

rn

rg

og

or

oa

om

om

oi

on

og

gr

ga

gm

gm

gi

gn

gg

ra

rm

rm

ri

rn

rg

am

am

ai

an

ag

mm

mi

mn

mg

mi

mn

mg

in

ig

ng

pro

prg

prr

pra

prm

prm

pri

prn

prg

rog

ror

roa

rom

rom

roi

ron

rog

ogr

oga

ogm

ogm

ogi

ogn

ogg

gra

grm

grm

gri

grn

grg

ram

ram

rai

ran

rag

amm

ami

amn

amg

mmi

mmn

mmg

min

mig

ing

prog

pror

proa

prom

prom

proi

pron

prog

rogr

roga

rogm

rogm

rogi

rogn

rogg

ogra

ogrm

ogrm

ogri

ogrn

ogrg

gram

gram

grai

gran

grag

ramm

rami

ramn

ramg

ammi

ammn

ammg

mmin

mmig

ming

progr

proga

progm

progm

progi

progn

progg

rogra

rogrm

rogrm

rogri

rogrn

rogrg

ogram

ogram

ograi

ogran

ograg

gramm

grami

gramn

gramg

rammi

rammn

rammg

ammin

ammig

mming

progra

progrm

progrm

progri

progrn

progrg

rogram

rogram

rograi

rogran

rograg

ogramm

ogrami

ogramn

ogramg

grammi

grammn

grammg

rammin

rammig

amming

program

program

prograi

progran

prograg

rogramm

rogrami

rogramn

rogramg

ogrammi

ogrammn

ogrammg

grammin

grammig

ramming

programm

programi

programn

programg

rogrammi

rogrammn

rogrammg

ogrammin

ogrammig

gramming

programmi

programmn

programmg

rogrammin

rogrammig

ogramming

programmin

programmig

rogramming

programming

ASCII Value Program in C

/*
 * C Program to find the ascii value of a character
 */
 
#include <stdio.h>
 
int main()
{
    char ch;
    printf("Enter Character: ");
    scanf("%c",&ch);
 
    //Type Casting character to int
    int ascii = (int)ch;
    printf("Ascii Value of %c is %d",ch,ascii);
    return 0;
}

Runtime Test Cases

Here is the runtime output of the C program to find the ascii value of a character when the user entered character is “c”.

Enter Character: c

Ascii Value of c is 99

/*
 * C Program to find the ascii value of all characters in a string
 */
 
#include <stdio.h>
 
int main()
{
    char str[100];
 
    //Input string
    printf("Enter String: ");
    gets(str);
    int i=0;
 
    //Iterating over string
    while(str[i]!='\0')
    {
        //Printing ascii value by typecasting
        printf("Ascii value of %c : %d\n",str[i],(int)str[i]);
        ++i;
    }
    return 0;
}

Runtime Test Cases

Here is the runtime output of the C program to find the ascii value of all characters in a string when the user entered string is “hello”.

Enter String: hello

Ascii value of h : 104

Ascii value of e : 101

Ascii value of l : 108

Ascii value of l : 108

Ascii value of o : 111

C Program to Find First and Last Occurrence of Character in a String

* 
 * C Program to find First and Last Occurrence of given 
 * Character in a String
 */
#include <stdio.h>
#include <string.h>
 
void main()
{
    int i, count = 0, pos1, pos2;
    char str[50], key, a[10];
 
    printf("enter the string\n");
    scanf(" %[^\n]s", str);
    printf("enter character to be searched\n");
    scanf(" %c", &key);
    for (i = 0;i <= strlen(str);i++)
    {
        if (key == str[i])
        {
            count++;
            if (count  == 1)
            {
                pos1 = i;
                pos2 = i;
                printf("%d\n", pos1 + 1);
            }
            else 
            {
                pos2 = i;
            }
        }
    }
    printf("%d\n", pos2 + 1);
}

Runtime Test Cases

enter the string

welcome to sanfoundry's c programming class!

enter character to be searched

m

6

34

C Program to Implement strpbrk() Function

/*

 * C Program to Implement a strpbrk() Function

 */

#include <stdio.h>

 

char* strpbrk(char *, char *);

 

int main()

{

    char string1[50], string2[50];

    char *pos;

 

    printf("Enter the String:\n");

    scanf(" %[^\n]s", string1);

    printf("\nEnter the Character Set:\n");

    scanf(" %[^\n]s", string2);

    pos=strpbrk(string1, string2);

    printf("%s", pos);

}

 

/* Locates First occurrence in string s1 of any character in string s2,

 * If a character from string s2 is found ,

 * a pointer to the character in string s1 is returned,

 * otherwise,  a NULL pointer is returned.

 */

char* strpbrk(char *string1, char *string2)

{

    int i, j, pos, flag = 0;

    for (i = 0; string1[i] != '\0';i++);

    pos = i;

    for (i = 0;string2[i] != '\0';i++)

    {

        for (j = 0;string1[j] != '\0';j++)

        {

            if (string2[i] == string1[j])

            {

                if (j <= pos)

                {

                    pos = j;   

                    flag = 1;   

                }

            }

         }       

    }

    if (flag == 1)

    {

        return &string1[pos];

    }

    else

    {

        return NULL;

    }

}

$gcc string34.c

$ a.out

Enter the String:

C programming Class

 

Enter the Character Set:

mp

programming Class