Skip to content

Latest commit

 

History

History
172 lines (121 loc) · 2.34 KB

22. C String Functions.md

File metadata and controls

172 lines (121 loc) · 2.34 KB

C String Functions

Video Link: https://youtu.be/XdnmsKUvGsc
Tutorial Link: https://www.programiz.com/c-programming/string-handling-functions

C String

#include <stdio.h>

int main() {

  char language[] = "C Programming";
  
  printf("%s", language);

  return 0;
}

Output

C Programming

1. strlen() Function

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

int main() {

  char language[] = "C Programming";
  
  printf("%s", language);

  printf("\nLength: %zu", strlen(language));

  return 0;
}

Output

C Programming
Length: 13

2. strcpy() Function

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

int main() {
  char food[] = "Pizza";

  char bestFood[strlen(food)];

  strcpy(bestFood, food);

  printf("%s", bestFood);

  return 0;
}

Output

Pizza

3. strcat() Function

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

int main() {

  char text1[] = "Hey, ";
  char text2[] = "How are you?";
  
  strcat(text1, text2);

  printf("%s", text1);

  return 0;
}

Output

Hey, How are you?

4. strcmp() Function

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

int main() {

  char text1[] = "abcd";
  char text2[] = "cdef";
  
  int result = strcmp(text1, text2);

  printf("%d", result);

  return 0;
}

Output

-2

Programming Task

Create a program to compare two strings and print the larger strings.Here is how the program should work:

  • Get two string input from the user using fgets()
  • Compare the length of both the strings using strlen()
  • Print the larger string

Solution

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

int main() {

  char food1[20];
  char food2[20];
  
  printf("Enter the first food: ");
  fgets(food1, sizeof(food1), stdin);
  
  printf("Enter the second food: ");
  fgets(food2, sizeof(food2), stdin);
  
  if (strlen(food1) > strlen(food2)) {
      printf("Longest Food Name: %s", food1);
  }
  else {
      printf("Longest Food Name: %s\n", food2);
  }
  
  return 0;
}

Output

Enter the first food: Coffee
Enter the second food: Tea
Longest Food Name: Coffee
Length: 7


Programiz Quiz

Q. Which of the following functions is used to join two strings?

Options:

  1. strjoin()
  2. strcat()
  3. join()
  4. cat()

Answer: 2