0% found this document useful (0 votes)
4 views11 pages

C c++ Example on String

The document provides examples of string manipulation in both C and C++, highlighting key differences in string handling between the two languages. It includes various operations such as input/output, length calculation, concatenation, comparison, and more advanced functions like substring extraction and character replacement. Overall, it serves as a comprehensive guide for understanding and working with strings in C and C++.

Uploaded by

Akash Satdeve
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views11 pages

C c++ Example on String

The document provides examples of string manipulation in both C and C++, highlighting key differences in string handling between the two languages. It includes various operations such as input/output, length calculation, concatenation, comparison, and more advanced functions like substring extraction and character replacement. Overall, it serves as a comprehensive guide for understanding and working with strings in C and C++.

Uploaded by

Akash Satdeve
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

C Example: Working with Strings

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

int main() {
char name[50]; // Declare a string (character array)

// Input a string
printf("Enter your name: ");
scanf("%s", name); // Reads input until space or newline
is encountered

// Output the string


printf("Hello, %s!\n", name);

// String operations
int length = strlen(name); // Get the length of the string
printf("Your name has %d characters.\n", length);

return 0;
}

Output:
Enter your name: Alice
Hello, Alice!
Your name has 5 characters.

C++ Example: Working with Strings


#include <iostream>
#include <string> // Include the string library

int main() {
std::string name; // Declare a string object

// Input a string
std::cout << "Enter your name: ";
std::cin >> name; // Reads input until space or newline is
encountered

// Output the string


std::cout << "Hello, " << name << "!" << std::endl;

// String operations
int length = name.length(); // Get the length of the
string
std::cout << "Your name has " << length << " characters."
<< std::endl;

return 0;
}

Output:

Enter your name: Alice


Hello, Alice!
Your name has 5 characters.

Key Differences:

1. String Handling:
o In C, strings are character arrays, and we need functions
like strlen for operations.
o In C++, strings are objects of the std::string class, which
provides member functions like .length().
2. Ease of Use:
o C++ offers more functionality and is easier to use for
string manipulation compared to C.

C Examples

1. String Input and Output


c
CopyEdit
#include <stdio.h>

int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str); // Input a string
printf("You entered: %s\n", str);
return 0;
}

Output:
Enter a string: Hello
You entered: Hello

2. String Length
#include <stdio.h>
#include <string.h>

int main() {
char str[] = "Hello, World!";
printf("Length of the string: %lu\n", strlen(str));
return 0;
}

Output:
Length of the string: 13

3. String Concatenation
#include <stdio.h>
#include <string.h>

int main() {
char str1[100] = "Hello";
char str2[] = ", World!";
strcat(str1, str2); // Concatenate str2 to str1
printf("Concatenated string: %s\n", str1);
return 0;
}

Output:
Concatenated string: Hello, World!

4. String Comparison
#include <stdio.h>
#include <string.h>

int main() {
char str1[] = "Hello";
char str2[] = "World";
int result = strcmp(str1, str2); // Compare str1 and str2
printf("Comparison result: %d\n", result);
return 0;
}
Output:
Comparison result: -1 (or another negative value indicating
str1 < str2)

5. String Copy
#include <stdio.h>
#include <string.h>

int main() {
char src[] = "Copy this";
char dest[50];
strcpy(dest, src); // Copy src into dest
printf("Copied string: %s\n", dest);
return 0;
}

Output:
Copied string: Copy this

C++ Examples

6. String Input and Output


#include <iostream>
#include <string>

int main() {
std::string str;
std::cout << "Enter a string: ";
std::cin >> str; // Input a string
std::cout << "You entered: " << str << std::endl;
return 0;
}

Output:
Enter a string: Hello
You entered: Hello

7. String Length
#include <iostream>
#include <string>

int main() {
std::string str = "Hello, World!";
std::cout << "Length of the string: " << str.length() <<
std::endl;
return 0;
}

Output:
Length of the string: 13

8. String Concatenation
#include <iostream>
#include <string>

int main() {
std::string str1 = "Hello";
std::string str2 = ", World!";
std::string result = str1 + str2; // Concatenate strings
std::cout << "Concatenated string: " << result <<
std::endl;
return 0;
}

Output:
Concatenated string: Hello, World!

9. Substring Extraction
#include <iostream>
#include <string>

int main() {
std::string str = "Hello, World!";
std::string sub = str.substr(7, 5); // Extract "World"
std::cout << "Substring: " << sub << std::endl;
return 0;
}

Output:
Substring: World
10. String Comparison
#include <iostream>
#include <string>

int main() {
std::string str1 = "Hello";
std::string str2 = "World";
if (str1 < str2) {
std::cout << str1 << " is less than " << str2 <<
std::endl;
} else {
std::cout << str1 << " is not less than " << str2 <<
std::endl;
}
return 0;
}

Output:
Hello is less than World

These examples cover the basics of string manipulation in both C and


C++. Let me know if you need more advanced examples!

C Examples

1. String Reversal
#include <stdio.h>
#include <string.h>

int main() {
char str[] = "Programming";
char reversed[50];
int len = strlen(str);

for (int i = 0; i < len; i++) {


reversed[i] = str[len - i - 1];
}
reversed[len] = '\0'; // Null-terminate the reversed
string

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


return 0;
}

Output:
Original: Programming
Reversed: gnimmargorP

2. Count Vowels
#include <stdio.h>
#include <string.h>

int main() {
char str[] = "Hello, World!";
int vowels = 0;

for (int i = 0; i < strlen(str); i++) {


if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' ||
str[i] == 'o' || str[i] == 'u' ||
str[i] == 'A' || str[i] == 'E' || str[i] == 'I' ||
str[i] == 'O' || str[i] == 'U') {
vowels++;
}
}

printf("Vowel count: %d\n", vowels);


return 0;
}

Output:
Vowel count: 3

3. Convert to Uppercase
#include <stdio.h>
#include <ctype.h>

int main() {
char str[] = "Hello, World!";

for (int i = 0; str[i] != '\0'; i++) {


str[i] = toupper(str[i]);
}

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


return 0;
}

Output:
Uppercase: HELLO, WORLD!

4. Find a Character in a String


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

int main() {
char str[] = "Programming";
char ch = 'g';
char *pos = strchr(str, ch);

if (pos) {
printf("Character '%c' found at index %ld\n", ch, pos
- str);
} else {
printf("Character '%c' not found\n", ch);
}

return 0;
}

Output:
Character 'g' found at index 3

5. Check Palindrome
#include <stdio.h>
#include <string.h>

int main() {
char str[] = "madam";
int len = strlen(str);
int isPalindrome = 1;

for (int i = 0; i < len / 2; i++) {


if (str[i] != str[len - i - 1]) {
isPalindrome = 0;
break;
}
}
if (isPalindrome) {
printf("'%s' is a palindrome.\n", str);
} else {
printf("'%s' is not a palindrome.\n", str);
}

return 0;
}

Output:
'madam' is a palindrome.

C++ Examples

6. Replace a Character
#include <iostream>
#include <string>

int main() {
std::string str = "Hello, World!";
char oldChar = 'o';
char newChar = 'a';

for (char &ch : str) {


if (ch == oldChar) {
ch = newChar;
}
}

std::cout << "Modified string: " << str << std::endl;


return 0;
}

Output:
Modified string: Hella, Warld!

7. String Find
#include <iostream>
#include <string>

int main() {
std::string str = "Programming in C++";
std::string word = "C++";
size_t pos = str.find(word);

if (pos != std::string::npos) {
std::cout << "Found '" << word << "' at position " <<
pos << std::endl;
} else {
std::cout << "'" << word << "' not found in the
string." << std::endl;
}

return 0;
}

Output:
Found 'C++' at position 16

8. String Append
#include <iostream>
#include <string>

int main() {
std::string str = "Hello";
str.append(", World!"); // Append a string
std::cout << "Appended string: " << str << std::endl;
return 0;
}

Output:
Appended string: Hello, World!

9. String Iteration
#include <iostream>
#include <string>

int main() {
std::string str = "Iteration";
std::cout << "Characters in the string: ";

for (char ch : str) {


std::cout << ch << " ";
}
std::cout << std::endl;
return 0;
}

Output:
Characters in the string: I t e r a t i o n

10. Convert String to Integer


#include <iostream>
#include <string>

int main() {
std::string str = "12345";
int number = std::stoi(str); // Convert string to integer
std::cout << "String: " << str << "\nConverted to integer:
" << number << std::endl;
return 0;
}

Output:
String: 12345
Converted to integer: 12345

You might also like