0% found this document useful (0 votes)
10 views8 pages

C# String Manipulation Techniques

The document contains 14 code snippets that demonstrate various string manipulation techniques in C#, including: 1. Reversing a string 2. Counting vowels and consonants in a string 3. Converting the first letter of a string to uppercase 4. Converting a string to lowercase and uppercase 5. Converting the first letter of each word to uppercase 6. Alternating uppercase and lowercase letters 7. Replacing a character with a special character 8. Replacing a character at a specific index 9. Counting the number of characters 10. Finding the longest and shortest words 11. Sorting an array 12. Creating a mirrored string 13. Replacing consonants with the

Uploaded by

Mamta Chauhan
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)
10 views8 pages

C# String Manipulation Techniques

The document contains 14 code snippets that demonstrate various string manipulation techniques in C#, including: 1. Reversing a string 2. Counting vowels and consonants in a string 3. Converting the first letter of a string to uppercase 4. Converting a string to lowercase and uppercase 5. Converting the first letter of each word to uppercase 6. Alternating uppercase and lowercase letters 7. Replacing a character with a special character 8. Replacing a character at a specific index 9. Counting the number of characters 10. Finding the longest and shortest words 11. Sorting an array 12. Creating a mirrored string 13. Replacing consonants with the

Uploaded by

Mamta Chauhan
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

1.

to reverse the given string



[Link]("Enter string");
string str = [Link]();
char[] ch = [Link]();
[Link](ch);
string str1 = new string(ch);
[Link]("String after reverse is:" + str1);
[Link]();

2. To count no of vowels and consonents in a given string

[Link]("Enter a string");
string str = [Link]();
str = [Link]();
int vow = 0, cons = 0;
foreach (char ch in str)
{
if (ch >= 'a' && ch <= 'z')
{

switch (ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u': vow++;
break;

default:
cons++;
break;

}
}
}
[Link]("vowels:" + vow);
[Link]("Consonants:" + cons);
[Link]();


[Link] convert the first letter of a string to upper case

[Link]("Enter new string");
string str = [Link]();
string s = [Link](0,1).ToUpper() + [Link](1);
[Link](s);
[Link]();

4. to convert string to lower case and vice versa

[Link]("Enter a string");
string str = [Link]();
string lower = [Link]();
string upper = [Link]();
[Link]("Lower case:"+lower);
[Link]("Upper case:"+upper);
[Link]();

5. To Convert starting letter of each word in a string to upper case

[Link]("Enter new string");
string str = [Link]();
string s =
[Link]([Link]());
[Link](s);
[Link]();

OR

[Link]("Enter new string");
string str = [Link]();
string[] s = [Link](' ');
string res = [Link];
int c = 0;
foreach (string a in s)
{
if (c == 0)
{
res = [Link](0, 1).ToUpper() + [Link](1);
c++;
}
else
{
res = res + " " + [Link](0, 1).ToUpper() + [Link](1);
}
}
[Link](res);
[Link]();


6. to convert alternate characters of a string to upper case

[Link]("Enter new string");
string str = [Link]();
char[] ch = [Link]();
char[] ch1 = new char[100];

int n = [Link];
int i=0;
while(i<n)
{
if (i % 2 == 0)
{
ch1[i] = [Link](ch[i]);
i++;
}
else
{
ch1[i] = [Link](ch[i]);
i++;
}
}
string fin = new string(ch1);
[Link]("Result is: "+fin);
[Link]();


7. Replacing give character with special character

[Link]("Enter a string");
string str = [Link]();
[Link]("Enter any character");
char ch = [Link]([Link]());
string res = [Link](ch, '@');
[Link]("After replacing: "+ res);
[Link]();

8. Replacing given index of a string with special character

[Link]("Enter a string");
string str = [Link]();
[Link]("Enter index value");
int n = [Link]([Link]());
[Link]("Enter special character");
char sp = [Link]([Link]());
char[] ch = [Link]();
ch[n] = sp;
string res = new string(ch);
[Link]("Result is:"+res);
[Link]();


9. To count no of characters in a string

[Link]("Enter any string");
string str = [Link]();
str=[Link]();
int count = 0;
foreach (char ch in str)
{
if (ch >= 'a' && ch <= 'z')
{
count++;
}
}
[Link]("No of characters are:"+count);
[Link]();

10. To find maximum and minimum length words in a given string

[Link]("Enter any string");
string str = [Link]();
string[] ar = [Link](' ');
int max = 0, min = 0;
string str1 = [Link];
string str2 = [Link];
max= ar[0].Length;
min= ar[0].Length;

foreach (string st in ar)
{
int n = [Link];
if (n >= max)
{
str1 = st;
max = n;
}
if (n <= min)
{
str2 = st;
min = n;

}
}
[Link]("max length word is:"+str1);
[Link]("Max length is:"+max);
[Link]("Min length word is:"+str2);
[Link]("Min length is:"+min);
[Link]();



11. To sort given array in ascending or descending order

[Link]("Enter size of array");
int n = [Link]([Link]());
int[] a = new int[n];
[Link]("Enter array elements");
for (int i = 0; i < n; i++)
{
a[i] = [Link]([Link]());
}
[Link]("Array in ascending order is:");

[Link](a);
[Link]("Array in ascending order:");
foreach (int k in a)
{
[Link](k + ",");
}
[Link](a);
[Link]("Array in descending order :");
foreach(int l in a)
{
[Link](l+",");
}
[Link]();


12. Creating mirror image of a string

[Link]("Enter a string");
string str = [Link]();
char[] ch = [Link]();
[Link](ch);
string str1 = new string(ch);
string res = str + '|' + str1;
[Link]("Result is:"+res);
[Link]();

13. Program to replace consonents with next alphabet

[Link]("Enter a string");
string str = [Link]();
char[] ch1 = [Link]();
char[] ch = new char[100];
int i = 0;
foreach (char c in ch1)
{

if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A'
|| c == 'E' || c == 'I' || c == 'O' || c == 'U')
{
ch[i] = c;
i++;
}
else
{
int z = c;
z++;
ch[i] = (char)z;
i++;
}
}
string res = new string(ch);
[Link]("Result is: "+res);
[Link]();




731-->(7-3)(3-1)-->42.

[Link]("Enter any number");
int n = [Link]([Link]());
string str = [Link]();
char[] ch = [Link]();
[Link](ch);
int[] res = new int[10];
string str1 = new string(ch);
int n1 = [Link](str1);
int x=0;
int prev = n1 % 10;
n1 = n1 / 10;
while (n1 != 0)
{

int next = n1 % 10;
n1 = n1 / 10;
res[x] = prev - next;
x++;
prev = next;
}
for (int i = 0; i < x; i++)
{
[Link](res[i]);
}
[Link]();




14. To print count of each character in an character array ** incomplete **

[Link]("Enter a string");
string str = [Link]();
char[] ch = [Link]();
char[] res = new char[100];
int[] res1 = new int[100];
[Link](ch);
int z = 0;
for (int i = 0; i < [Link]; i++)
{
int count = 0;
char temp = ch[i];
for (int j = i; j < [Link]; j++)
{
if (temp == ch[j])
{
i++;
count++;
}
}
res[z] = temp;
z++;
res1[z] = count;
z++;
count = 0;
}
for(int g=0;g<z;g++)
{
[Link](res[g] + ",");
}
[Link]();


[Link] check weather both the strings contains same characters in different
order

[Link]("Enter string1");
string str1 = [Link]();
[Link]("Enter string2");
string str2 = [Link]();
char[] ch1 = [Link]();
[Link](ch1);
char[] ch2 = [Link]();
[Link](ch2);
string str3 = new string(ch1);
string str4 = new string(ch2);
int n = [Link](str3, str4);
if (n == 0)
{
[Link]("Both strings are same");
}
else
{
[Link]("Both are different strings");
}
[Link]();


16. To replace alternate characters with '*'

[Link]("Enter new string");
string str = [Link]();
char[] ch = [Link]();
char[] ch1 = new char[100];

int n = [Link];
int i = 0;
while (i < n)
{
if (i % 2 == 0)
{
ch1[i] = ch[i];
i++;
}
else
{
ch1[i] = '*';
i++;
}
}
string fin = new string(ch1);
[Link]("Result is: " + fin);
[Link]();


17. Remove duplicate characters in an array

[Link]("Enter a string");
string str = [Link]();
char[] ch = [Link]();
char[] ch1 = new char[20];
[Link](ch);
int k = 0;
char temp = ch[0];
for (int i = 0; i < [Link]; i++)
{

for (int j = i; j < [Link]; j++)
{
if (ch[j] == temp)
{
i++;
}
}
temp = ch[i];
ch1[k] = temp;
k++;

}
[Link]("Array after removing duplicates:");
for (int i = 0; i < k; i++)
{
[Link](ch1[i] + ",");
}
[Link]();


18. Same as program6

Common questions

Powered by AI

The method involves converting the string to lowercase to ensure uniformity and then iterating through each character. It checks if each character falls between 'a' and 'z'. Using a switch case, it increments the vowel count for any of 'a', 'e', 'i', 'o', 'u', and increments the consonant count for all other letters .

The process involves converting the string to a char array, modifying the element at the specified index with the special character, and then constructing the final string from the modified array. This approach allows precise character manipulation at specified positions .

The program capitalizes each word's first letter by using TextInfo.ToTitleCase found in the current cultural context via Thread.CurrentThread.CurrentCulture. This method respects cultural-specific casing rules, ensuring that the capitalization aligns with linguistic conventions, such as different capitalization rules in various languages .

The program converts the first letter to uppercase by taking the substring of the first character and applying ToUpper(), then concatenating it with the rest of the string obtained via Substring(1). This affects only the first letter, leaving the rest of the string unchanged .

The program checks for anagrams by first converting each string to a char array, sorting the arrays, and then reconstructing strings from these sorted arrays. If the newly formed strings are identical, the original strings are anagrams. This method effectively checks for character order and frequency while ignoring specific positions .

The procedure involves using ToLower() and ToUpper() methods on the input string. First, the string is converted entirely to lowercase with ToLower(), and then to uppercase with ToUpper(). These methods provide a simple way to uniform the text case for different requirements .

The program takes user input for both the string and the character to be replaced. It utilizes the Replace() method to substitute every occurrence of the specified character with '@', resulting in a modified string without altering the character sequence .

The program reverses the string by first converting it to a char array using the ToCharArray() method. Then, it uses Array.Reverse() to reverse the order of elements in the array. Finally, a new string is created from the reversed char array with new string(ch). This approach leverages the mutable nature of arrays to manipulate the string efficiently .

The program iterates through the string using a loop with an index variable, checking if the index is even or odd to decide the case transformation. It uses char.ToUpper() for characters at even indices and char.ToLower() for odd ones, storing the results in a separate array for output .

The program splits the string into an array of words and iterates through the array to compare each word's length. It keeps track of the maximum and minimum lengths found, updating the respective words and lengths accordingly. The results are then printed with their word and respective lengths .

You might also like