String operations in C, C++, Java and Python

Program to perform all the basic string operations is discussed here. Some of the basic operations are printing the string, printing the length of the string, reversing a string, concatenating two strings, and so on.

For example, consider two strings

str1 = “Focus”
str2 = “Academy”

Length of str1 is 5
Length of str2 is 7
Reverse of str1 is sucoF
Reverse of str2 is ymedacA
Concatenation of str1 and str2 is FocusAcademy

String Operations | Program to print a string

/* C program to print a string */
#include <stdio.h>
#include <string.h>
int main()
{
char a[100];
int l;
printf(“\nEnter the string : “);
scanf(“%s”,&a);
printf(“The string is %s “, a);
return 0;
}

/* C++ program to print a string */
#include <iostream>
using namespace std;
int main()
{
string a;
int l;
cout << “\nEnter the string : “;
cin >> a;
cout << “The string is ” << a;
return 0;
}

/* Java Program to print a string */
import java.util.*;
public class Main
{
public static void main(String[] args)
{
String a;
Scanner sc = new Scanner(System.in);
a = sc.nextLine();
System.out.println(“The string is ” + a);
}
}

# Python program to print a string

a = input()
print(“The string is”,a)

String Operations | Program to print the length of a string

/* C program to print the length of a string */
#include <stdio.h>
#include <string.h>
int main()
{
char a[100];
int l;
scanf(“%s”,&a);
int i;
l = strlen(a);
printf(“\nThe length of %s is %d\n”,a,l);
return 0;
}

/* C++ program to print the length of a string */
#include <iostream>
#include <string.h>
using namespace std;

int main()
{
char a[100];
int l;
cin >> a;
int i;
l = strlen(a);
cout << “\nThe length of the string is ” << l << endl;
return 0;
}

/* Java Program to print the length of a string */
import java.util.*;
public class Main
{
public static void main(String[] args)
{
String a;
Scanner sc = new Scanner(System.in);
a = sc.nextLine();
System.out.println(“The length of the string is ” + a.length());
}
}

# Python program to print the length of te string
a = input()
print(“The length of the string is”,len(a))

String Operations | Program to copy a string

/* C program to copy a string */

/* C program to copy a string */
#include <stdio.h>
#include <string.h>
int main()
{
char a[100], b[100];
int l;
scanf(“%s”,&a);
strcpy(b, a);
printf(“\String 1 : %s\nString 2 : %s\n”,a, b);
return 0;
}

/* C++ program to copy a string */
#include <iostream>
#include <string.h>
using namespace std;

int main()
{
char a[100], b[100];
int l;
cin >> a;
strcpy(b, a);
cout << “\String 1 : ” << a << endl << “String 2 : ” << b << endl;
return 0;
}

/* Java Program to copy a string */
import java.util.*;
public class Main
{
public static void main(String[] args)
{
String a;
Scanner sc = new Scanner(System.in);
a = sc.nextLine();
String b = new String(a);
System.out.println(“String 1 : ” + a + “\nString 2 : ” + b);
}
}

# Python program to copy a string
a = input()
b = a
print(“String 1 :”,a,”\nString 2 :”,b)

String Operations | Program to reverse a string

/* C program to reverse a string */
#include <stdio.h>
#include <string.h>
int main()
{
char a[100], b[100];
int l;
scanf(“%s”,&a);
int i;
printf(“Reversed String : “);
for(i = strlen(a); i >= 0; i–)
{
printf(“%c”,a[i]);
}
return 0;
}

/* C program to reverse a string */
#include <iostream>
#include <string.h>
using namespace std;

int main()
{
char a[100];
int l;
cin >> a;
int i;
cout << “Reversed String : “;
for(i = strlen(a); i >= 0; i–)
{
cout << a[i];
}
return 0;
}

/* Java Program to reverse a string */
import java.util.*;
public class Main
{
public static void main(String[] args)
{
String a;
int i;
Scanner sc = new Scanner(System.in);
a = sc.nextLine();
StringBuilder sb = new StringBuilder(a);
System.out.print(“Reversed String : “);
System.out.println(sb.reverse().toString());
}
}

# Python program to reverse a string
def reverse(s):
str = “”
for i in s:
str = i + str
return str

s = input()

print (“The original string is : “,end=””)
print(s)
print (“The reversed string is : “,end=””)
print (reverse(s))

String Operations | Program to concatenate two strings

/* C program to concatenate two strings */
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = “Face “, str2[] = “Prep”;
strcat(str1,str2);
printf(“After concatenation : “);
puts(str1);

return 0;
}

/* C++ program to concatenate two strings */
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char str1[] = “Face “, str2[] = “Prep”;
strcat(str1,str2);
cout << “After concatenation : “;
cout << str1;

return 0;
}

/* Java Program to concatenate two strings */
import java.util.*;
public class Main
{
public static void main(String[] args)
{
String a, b;
int i;
Scanner sc = new Scanner(System.in);
a = sc.nextLine();
b = sc.nextLine();
System.out.println(“Concatenated strings : “+ a + b);

}
}

# Python program to concatenate two strings
a = input()
b = input()
print(“Concatenated strings : “,a+b)

String Operations | Program to compare two strings

/* C program to compare two strings */
#include <stdio.h>
#include <string.h>

int main()
{
char str1[100], str2[100];
scanf(“%s %s”,str1,str2);
strcmp(str1,str2);
if(strcmp(str1,str2) == 0)
printf(“The strings are equal”);
else
printf(“The strings are not equal”);
return 0;
}

/* C++ program to compare two strings */
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char str1[100], str2[100];
cin >> str1 >> str2;
strcmp(str1,str2);
if(strcmp(str1,str2) == 0)
cout << “The strings are equal”;
else
cout << “The strings are not equal”;
return 0;
}

/* Java Program to compare two strings */
import java.util.*;
public class Main
{
public static void main(String[] args)
{
String a, b;
int i;
Scanner sc = new Scanner(System.in);
a = sc.nextLine();
b = sc.nextLine();
if(a.equals(b))
System.out.println(“The strings are equal”);
else
System.out.println(“The strings are not equal”);
}
}

# Python program to compare two strings
a = input()
b = input()
if(a == b):
print(“The strings are equal”)
else:
print(“The strings are not equal”)

Output:

basic string operations