Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Breaking

Post Top Ad

Your Ad Spot

Saturday, March 4, 2023

Array of strings in c

 Array of strings in c


Array of strings in c


In the world of programming, arrays are one of the most fundamental data structures. They allow developers to store and manipulate multiple values of the same type in a single variable. In C programming, arrays of strings are a commonly used data structure, which allows developers to work with multiple strings at once. In this article, we will explore arrays of strings in C programming.

An array of strings in c is a collection of strings that are stored as a contiguous block of memory. Each element of the array contains a string of characters, which can be accessed individually or as a whole. Array of strings in c are part of the C standard library, which is included in all C programs by default.

Here are some of the most commonly used string functions in C programming:

 

1. strlen();

This function gives the number of characters as an output, excluding the terminating null character. A string constant or array of characters can be passed as arguments for this function.

Syntax:  strlen(argument);

Example: strlen(“Computer”);

Output: 8

 

Example:

#include <stdio.h>

#include <string.h>

int main() {

   char str[] = "Hello World";

   int len = strlen(str);

   printf("Length of string: %d", len);

   return 0;

}

 

Output:

Length of string: 11

 

2. strcat():

The strcat() function is used to concatenate one string to another. It takes two arguments: the destination string and the source string. The function appends the source string to the destination string, including the null terminator.

Syntax: strcat(argument1,argument2);

Example: strcat(“Computer”, “Science”);

Output: ComputerScience

 

Example:

#include <stdio.h>

#include <string.h>

int main() {

   char dest[20] = "Hello";

   char src[] = " World";

   strcat(dest, src);

   printf("Concatenated string: %s", dest);

   return 0;

}

 

Output:

Concatenated string: Hello World

 

3. strcmp():

The strcmp() function is used to compare two strings in C. It takes two arguments: the first string and the second string. The function returns an integer value that represents the comparison result. If the first string is greater than the second string, the function returns a positive integer. If the first string is less than the second string, the function returns a negative integer. If the strings are equal, the function returns 0.

Syntax: strcmp(string1, string2);

Example: strcmp(“Computer”, “Computer”);

Output: 0

 

Example:

#include <stdio.h>

#include <string.h>

int main() {

   char str1[] = "Hello";

   char str2[] = "World";

   int result = strcmp(str1, str2);

   if(result < 0) {

      printf("String 1 is less than String 2");

   }

   else if(result > 0) {

      printf("String 1 is greater than String 2");

   }

   else {

      printf("String 1 is equal to String 2");

   }

   return 0;

}

 

Output:

String 1 is less than String 2

 

 4. strrev():

The strrev() function in C programming is used to reverse a given string. It is a standard library function that is defined in the string.h header file. The function takes a single argument, which is a pointer to the string to be reversed. The string is modified in-place, and the function returns a pointer to the reversed string.

Syntax: strrev(argument);

Example: strrev(“Computer”);

Output: retupmoC

 

Example:

#include <stdio.h>

#include <string.h>

int main() {

   char str[] = "Hello, World!";

   printf("Original string: %s\n", str);

   strrev(str);

   printf("Reversed string: %s\n", str);

   return 0;

}

 

Output:

Original string: Hello, World!

Reversed string: !dlroW ,olleH

 

5. strcpy();

The strcpy() function in C programming is used to copy the contents of one string into another string. It is a standard library function that is defined in the string.h header file. The function takes two arguments, which are pointers to the source and destination strings.

Syntax: strcpy(destination, source);

Example: strcpy(string, “Computer”);

 

Example:

#include <stdio.h>

#include <string.h>

int main() {

char src[] = "Hello, World!";

   char dest[50];

   strcpy(dest, src);

   printf("Source string: %s\n", src);

   printf("Destination string: %s\n", dest);

   return 0;

}

 

Output:

Source string: Hello, World!

Destination string: Hello, World!

 

6. strlwr():

This function is used to convert the capital letter (uppercase) character into a small letter (lowercase). If any of the characters are already in lowercase then it skips that character.

Syntax: strlwr(argument);

Example: strlwr(“COMPUTER”);

Output: computer

 

/* This program converts lowercase into uppercase*/

#include<stdio.h>

#include<conio.h>

#include<string.h>

int main()

{

  char str[30];

  clrscr();

  printf("Enter uppercase string;");

  scanf("%s",str);

  strlwr(str);

  printf("The lowercase string:\n %s",str);

return 0;

}

 

7. strupr():

This function is used to convert the small letter (lowercase) character into capital letter (uppercase). If any of the character is already in uppercase then it skip that character.

Syntax: strupr(argument);

Example: strupr(“computer”);

Output: COMPUTER

 

/* This program is for converting lowercase into uppercase*/

#include<stdio.h>

#include<string.h>

int main()

{

  char str[30];

  clrscr();

  printf("Enter uppercase string;");

  scanf("%s",str);

  strupr(str);

  printf("The lowercase string:\n %s",str);

return 0;

}

 

In C programming, strings are represented as arrays of characters, with the last character in the string being a null character (\0). Therefore, when working with arrays of strings, we need to be careful to ensure that each string is properly terminated with a null character.

 

Conclusion:

Arrays of strings in c are a powerful data structure, which allows developers to store and manipulate multiple strings at once. By understanding how to declare, initialize, and access elements of arrays of strings, developers can write efficient and effective C programs.


You may Read:

C program to check whether the entered number is divisible by 5 but not by 11

C program to read roll no and name of students by using array of structure

C program to sort names in alphabetical ascinding order using-structures

Array in JavaScript

No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages