Related Topics

Data Structure
StringBuffer sb = new StringBuffer();
You can also initialize a StringBuffer
with a string as shown below:
String str = "hello";
StringBuffer sb = new StringBuffer(str);
In this case, the StringBuffer
will contain the same sequence of characters as the string “hello”.
StringBuffer sb = new StringBuffer("Hello");
sb.append(" world");
System.out.println(sb.toString()); // Output: Hello world
In the above example, we first create a StringBuffer
with the initial value “Hello”. We then call the append()
method on the StringBuffer
instance, passing in the string ” world” as an argument. This appends the new string to the end of the existing string buffer, resulting in the value “Hello world”. Finally, we call the toString()
method on the StringBuffer
instance to convert it to a String
object and print it to the console.
public StringBuffer insert(int offset, char c)
public StringBuffer insert(int offset, CharSequence cs)
public StringBuffer insert(int offset, CharSequence cs, int start, int end)
Here, offset
is the position where the insertion should start, c
is the character to be inserted, cs
is the character sequence to be inserted, and start
and end
are the start and end indexes of the character sequence to be inserted.
For example, the following code snippet inserts the string “world” into the string buffer at position 5:
StringBuffer sb = new StringBuffer("Hello ");
sb.insert(5, "world");
System.out.println(sb); // Output: Hello world
StringBuffer sb = new StringBuffer("Hello, World!");
sb.delete(0, 6); // Deletes "Hello, " from the string buffer
System.out.println(sb.toString()); // Output: World!
sb.deleteCharAt(0); // Deletes the first character 'W' from the string buffer
System.out.println(sb.toString()); // Output: orld!
// Create a StringBuffer
StringBuffer sb = new StringBuffer("Hello, World!");
// Reverse the contents of the StringBuffer
sb.reverse();
// Print the reversed string
System.out.println(sb.toString());
Output:
!dlroW ,olleH
In this example, the reverse()
method is called on the StringBuffer
object sb
, which reverses the order of characters in the buffer. The reversed string is then printed to the console using the toString()
method.
char myString[] = "Hello, world!";
This declares a character array named myString
and initializes it with the string "Hello, world!"
. The size of the array is automatically determined based on the length of the string plus one for the null character.
Alternatively, you can declare a string separately and then initialize it using the strcpy()
function from the string.h
library:
char myString[20];
strcpy(myString, "Hello, world!");
This declares an array named myString
with a size of 20 characters and then initializes it with the string "Hello, world!"
using strcpy()
.
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello ";
char str2[] = "World!";
strcat(str1, str2);
printf("Concatenated string is: %s\n", str1);
return 0;
}
This program declares two strings str1
and str2
, with str1
initialized to "Hello "
and str2
initialized to "World!"
. Then, the strcat()
function is used to append str2
to str1
, resulting in str1
containing "Hello World!"
. The resulting string is then printed to the console.
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, world!";
int len = strlen(str);
printf("Length of '%s' is %d\n", str, len);
return 0;
}
Output:
Length of 'Hello, world!' is 13
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello";
char str2[20] = "World";
if (strcmp(str1, str2) == 0) {
printf("The strings are equal.\n");
} else {
printf("The strings are not equal.\n");
}
return 0;
}
In this example, the strcmp()
function is used to compare the strings "Hello"
and "World"
. Since the strings are not equal, the program prints "The strings are not equal."
.
#include <stdio.h>
#include <string.h>
void reverse(char str[]) {
int start = 0;
int end = strlen(str) - 1;
char temp;
while (start < end) {
temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
}
int main() {
char str[] = "hello";
printf("Before: %s\n", str);
reverse(str);
printf("After: %s\n", str);
return 0;
}
Output:
Before: hello
After: olleh
In this example, the reverse()
function takes a string as a parameter and swaps the characters at opposite ends of the string until the middle is reached. The start
and end
variables are initialized to the first and last characters of the string, respectively. The temp
variable is used to hold the value of one of the characters while it is being swapped. The loop continues as long as start
is less than end
. Within the loop, the characters at start
and end
are swapped, start
is incremented, and end
is decremented. Finally, the main()
function demonstrates how to call the reverse()
function and print the results.
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
char substr[] = "World";
if (strstr(str, substr) != NULL) {
printf("Substring found!\n");
} else {
printf("Substring not found.\n");
}
return 0;
}
In this example, strstr()
searches the str
string for the substr
substring. If substr
is found in str
, strstr()
returns a pointer to the beginning of the substring in str
. If substr
is not found in str
, strstr()
returns NULL
.
Here’s an example of using strchr()
to check if a string contains a specific character:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
char ch = 'W';
if (strchr(str, ch) != NULL) {
printf("Character found!\n");
} else {
printf("Character not found.\n");
}
return 0;
}
In this example, strchr()
searches the str
string for the ch
character. If ch
is found in str
, strchr()
returns a pointer to the first occurrence of ch
in str
. If ch
is not found in str
, strchr()
returns NULL
.




Popular Category
Topics for You
Go through our study material. Your Job is awaiting.