LATEST

Strings in C/C++ with examples

In the previous tutorial, we learned about strings in C/C++. In this tutorial, we will learn about strings in C/C++ with examples. This is one of the basic knowledge in C/C++ programming language, so please learn it carefully.

Top 10 strings in C/C++ with example

Here, we will implement examples using strings in C/C++ from basic to advanced. For each example, we will implement with two different languages C and C++.

Example 1: Import and export strings in C/C++

C++ programming:

#include <iostream>
#include <string>
using namespace std;

int main()
{
   char str1[100];
   char str2[100];
   char str3[100];
   cout<<"Enter string str1: ";
   cin>>str1;
   cout<<"Enter string str2: ";
   cin>>str2;
   cout<<"Enter string str3: ";
   cin>>str3;

   cout<<"str1: "<<str1<<endl;
   cout<<"str2: "<<str2<<endl;
   cout<<"str3: "<<str3<<endl;

  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>

int main(void) {
   char str1[100];
   char str2[100];
   char str3[100];
   printf("Enter string str1: ");
   scanf("%s", str1);
   printf("Enter string str2: ");
   scanf("%s", str2);
   printf("Enter string str3: ");
   scanf("%s", str3);

   printf("str1: %s\n",str1);
   printf("str2: %s\n",str2);
   printf("str3: %s\n",str3);

  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

Example 2: Reverse strings in C/C++

C++ programming:

#include <iostream>
#include <string>
using namespace std;

int Length(char s[])
{
    int i = 0;
    while (s[i] != NULL)
        ++i;
    return i - 1;
}
 
char* Reverse(char str[])
{
    int length = Length(str);
    char* temp = new char[length + 1];
    for (int i = 0; i < length; i++)
    {
        temp[i] = str[length - 1 - i];
    }
    temp[length] = NULL;
    return temp;
}
 
void PrintfReverse(char str[]) {
    int length = Length(str);
    cout<<"\nString after reverse: ";
    for (int i = length - 1; i >= 0; i--)
    {
        cout<<str[i];
    }
}

int main()
{
  char s[100];
  cout<<"\nEnter a string: ";
  fgets(s, 100, stdin);
  char* kq = Reverse(s);
  PrintfReverse(s);

  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>
#include <stdbool.h>

int Length(char s[])
{
    int i = 0;
    while (s[i] != NULL)
        ++i;
    return i - 1;
}
 
char* Reverse(char str[])
{
    int length = Length(str);
    char* temp = new char[length + 1];
    for (int i = 0; i < length; i++)
    {
        temp[i] = str[length - 1 - i];
    }
    temp[length] = NULL; 
    return temp;
}
 
void PrintfReverse(char str[]) {
    int length = Length(str);
    printf("\nString after reverse: ");
    for (int i = length - 1; i >= 0; i--)
    {
        printf("%c", str[i]);
    }
}

int main(void) {
  char s[100];
  printf("\nEnter a string: ");
  fgets(s, 100, stdin);
  char* kq = Reverse(s);
  PrintfReverse(s);

  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

Example 3: Concatenate two strings in C/C++

C++ programming:

#include <iostream>
#include <string>
using namespace std;

int main()
{
   char str1[100], str2[100], i, j;
   cout<<"Enter first string: ";
   cin>>str1;

   cout<<"Enter second string: ";
   cin>>str2;
   for(i=0; str1[i]!='\0'; ++i); 
   for(j=0; str2[j]!='\0'; ++j, ++i)
   {
      str1[i]=str2[j];
   }
   str1[i]='\0';
   cout<<"Result: "<<str1;

  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>
#include <stdbool.h>

int main(void) {
   char str1[100], str2[100], i, j;
   printf("Enter first string: ");
   scanf("%s", &str1);

   printf("Enter second string: ");
   scanf("%s", &str2);
   for(i=0; str1[i]!='\0'; ++i); 
   for(j=0; str2[j]!='\0'; ++j, ++i)
   {
      str1[i]=str2[j];
   }
   str1[i]='\0';
   printf("Result: %s",str1);

  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

Example 4: Find the length of strings in C/C++

C++ programming:

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
   char str[100];
   int length;
   cout<<"Enter a string: ";
   cin>>str;
   length = strlen(str);

   cout<<"The length of the string is: "<<length;

  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>
#include <stdbool.h>

int main(void) {
   char str[100];
   int length;
   printf("Enter a string: ");
   scanf("%s", str);
   length = strlen(str);

   printf("The length of the string is: %d",length);

  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

Example 5: Sort strings in C/C++ in Alphabetical order

C++ programming:

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
   int i,j,count;
   char str[25][25],temp[25];
   cout<<"How many strings do you want to enter: ";
   cin>>count;
   cout<<"Enter the strings: \n";
   for(i=0;i<count;i++)
      cin>>str[i];
   for(i=0;i<count;i++)
      for(j=i+1;j<count;j++){
         if(strcmp(str[i],str[j])>0){
            strcpy(temp,str[i]);
            strcpy(str[i],str[j]);
            strcpy(str[j],temp);
         }
      }
   cout<<"The strings after sorting are:\n";
   for(i=0;i<=count;i++)
      puts(str[i]);

  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>

int main(void) {
  int i,j,count;
   char str[25][25],temp[25];
   printf("How many strings do you want to enter: ");
   scanf("%d", &count);
   printf("Enter the strings: \n");
   for(i=0;i<count;i++)
      scanf("%s", &str[i]);
   for(i=0;i<count;i++)
      for(j=i+1;j<count;j++){
         if(strcmp(str[i],str[j])>0){
            strcpy(temp,str[i]);
            strcpy(str[i],str[j]);
            strcpy(str[j],temp);
         }
      }
   printf("The strings after sorting are:\n");
   for(i=0;i<=count;i++)
      puts(str[i]);

  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

Example 6: Convert uppercase strings to lowercase strings in C/C++

C++ programming:

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
   char str[25];
   int i;
   cout<<"Enter a string: ";
   fgets(str,25,stdin);
   for(i=0;i<=strlen(str);i++){
      if(str[i]>=65&&str[i]<=90)
         str[i]=str[i]+32;
   }
   cout<<"After converting to lowercase: "<<str;

  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>

int main(void) {
  char str[25];
   int i;
   printf("Enter a string: ");
   fgets(str,25,stdin);
   for(i=0;i<=strlen(str);i++){
      if(str[i]>=65&&str[i]<=90)
         str[i]=str[i]+32;
   }
   printf("After converting to lowercase: %s ",str);

  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

Example 7: Convert lowercase strings to uppercase strings in C/C++

C++ programming:

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
   char str[25];
   int i;
   cout<<"Enter a string: ";
   fgets(str,25,stdin);
   for(i=0;i<=strlen(str);i++){
      if(str[i]>=97&&str[i]<122)
         str[i]=str[i]-32;
   }
   cout<<"After converting to uppercase: "<<str;

  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>

int main(void) {
  char str[25];
   int i;
   printf("Enter a string: ");
   fgets(str,25,stdin);
   for(i=0;i<=strlen(str);i++){
      if(str[i]>=97&&str[i]<=122)
         str[i]=str[i]-32;
   }
   printf("After converting to uppercase: %s ",str);

  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

Example 8: Compare two strings in C/C++

C++ programming:

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
  char lhs[100];
  char rhs[100];
  cout<<"Enter the first string: ";
  cin>>lhs;
  cout<<"Enter the second string: ";
  cin>>rhs;
  int result;
  result = strcmp(lhs,rhs);
  if(result != 0)
    cout<<lhs<< " and " << rhs <<" different";
  else
    cout<<lhs<< " and " << rhs <<" alike";
  cout<<endl<<endl;
  result = strcmp(lhs,lhs);
  if(result != 0)
    cout<<lhs<< " and " << lhs <<" different";
  else
    cout<<lhs<< " and " << lhs <<" alike";

  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>

int main(void) {
   char lhs[100];
  char rhs[100];
  printf("Enter the first string: ");
  scanf("%s", lhs);
  printf("Enter the second string: ");
  scanf("%s", rhs);
  int result;
  result = strcmp(lhs,rhs);
  if(result != 0)
    printf("%s and %s different", lhs,rhs);
  else
    printf("%s and %s alike", lhs,rhs);
  printf("\n\n");
  result = strcmp(lhs,lhs);
  if(result != 0)
    printf("%s and %s different", lhs,lhs);
  else
    printf("%s and %s alike", lhs,lhs);

  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

Example 9: Searching for ch character in string in C/C++

C++ programming:

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
  char str[100];
  char ch;
  cout<<"Enter a string: ";
  fgets(str, 100, stdin);
  cout<<"Enter a characters: ";
  cin>>ch;
  
  if (strchr(str, ch))
        cout << "Characters " << ch << " exist in string \"" << str << "\"";
  else
        cout << "Characters " << ch << " not exist in string \"" << str << "\"";

  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>

int main(void) {
  char str[100];
  char ch;
  printf("Enter a string: ");
  fgets(str, 100, stdin);
  printf("Enter a characters: ");
  scanf("%c", &ch);
  
  if (strchr(str, ch))
        printf("Characters %c exist in string \"%s\"",ch,str);
  else
        printf("Characters %c not exist in string \"%s\"",ch,str);

  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

Example 10: Checks for the existence of a string str in strings in C/C++.

C++ programming:

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
  char str1[100];
  char str2[100];
  cout<<"Enter the first string: ";
  cin>>str1;
  cout<<"Enter the second string: ";
  cin>>str2;
  
  if (strstr(str1, str2))
        cout<<"String \""<<str1<<"\" exist in string \""<<str2<<"\"";
  else
        cout<<"String \""<<str1<<"\" not exist in string \""<<str2<<"\"";

  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>

int main(void) {
  char str1[100];
  char str2[100];
  printf("Enter the first string: ");
  scanf("%s", &str1);
  printf("Enter the second string: ");
  scanf("%s", &str2);
  
  if (strstr(str1, str2))
        printf("String \"%s\" exist in string \"%s\"",str1,str2);
  else
        printf("String \"%s\" not exist in string \"%s\"",str1,str2);

  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

Cùng chuyên mục:

Arrays in C / C++ with examples

Arrays in C / C++ with examples

Switch...case statement in C / C++ with example

Switch...case statement in C / C++ with example

Multiple conditions for if statement in C / C++

Multiple conditions for if statement in C / C++

Can if statement have 2 conditions? Can you have 3 conditions in an if statement?…

if - else statement in C / C++ with example

if - else statement in C / C++ with example

Do while loop in C / C++ with examples

Do while loop in C / C++ with examples

While loop in C / C++ with examples

While loop in C / C++ with examples

For loop in C / C++ with examples

For loop in C / C++ with examples

Top