Arrays And Strings In C Programming

Arrays And Strings in 'C'

Arrays And Strings In C Programming
Arrays and Strings in C

Strings can be represented as a one-dimensional character-type array. Each character within the string will be stored within one element of the array.

Most C compilers include library functions that allow strings to be compared, copied or concatenated.

The following program makes use of the library functions strcmp() (to compare two strings, it does not distinguish between upper & lowercase) and strcpy() (to copy one string to another).

 

Program -1

#include <stdio.h>

#include<stdlib.h>
 
#include<string.h>

/* Sort A List Of Words. Using A Two-Dimensional Character Array */
 
void main()

{          int element, n=0;        

            char word[10][12];

            void sort(int n,char word[][12]);

            printf("Enter One Word Per Line\n");

            printf("Type \'END\'when finished\n\n");

 

do               printf("Word[%d] : "' n+ 1);
                      scanf("%s",word[n]);
                      }
 

while(strcmp(word[n++], "END"));

sort(--n,word);

printf("\nSorted List of Words :\n");

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

printf("\nWord[%d] : %s", element+1, word[element]);

}

 

void sort(int n, char word [][12])

{       char temp[12]

         int element, item;

         for(item=0; item<n-1 ; ++item

         for(element=item+1; element<n; ++element)

         if(strcmp(word[item], word[element]) > 0 )

        {       strcpy(temp, word[item]);

                 strcpy(word[item]), word[element]); 

                 strcpy(word[element], temp);  

                

        return

        }

 

 

Read Also:

 

Exercise regularly. Eat a nutritious diet. Don't smoke - WHO (Covid-19).

 

Let's see also...

 

Thanks a lot for reading.
Neel Kamal
Arrays And Strings In C Programming Arrays And Strings In C Programming Reviewed by Neel Kamal on October 24, 2021 Rating: 5

No comments:

For More Details Subscribe & Comment..!

Powered by Blogger.