String Algorithma and code examples
String Algorithma and code examples
1. Initialize variables:
o Let i = 0 (index for traversing destination).
o Let j = 0 (index for traversing source).
2. Traverse the destination string:
o While destination[i] is not the null character ('\0'):
Increment i to move to the next character.
o At the end of this step, i points to the null character of the
destination string.
3. Copy characters from the source string:
o While source[j] is not the null character ('\0'):
Copy source[j] to destination[i].
Increment both i and j to move to the next positions.
4. Terminate the destination string:
o Assign the null character ('\0') to destination[i].
5. End:
o The destination string now contains its original content followed
by the content of the source string.
Input:
destination = "Hello, "
source = "World!"
Steps:
1. Traverse destination to find the null character ('\0').
o i reaches index 7 (end of "Hello, ").
2. Copy characters from source:
o source[0] = 'W' → destination[7] = 'W'
o source[1] = 'o' → destination[8] = 'o'
o ... (continue until source[j] = '\0').
3. Add null character:
o destination[13] = '\0'.
Output:
destination = "Hello, World!"
Code
#include <stdio.h>
int main() {
char str1[100] = "Hello, "; // Destination string with extra space
char str2[] = "World!"; // Source string
printf("Before appending:\n");
printf("String 1: %s\n", str1);
printf("String 2: %s\n", str2);
printf("\nAfter appending:\n");
printf("String 1: %s\n", str1);
return 0;
}
ASCII Representation for Uppercase and Lowercase Letters
In ASCII, the alphabetic characters are represented with distinct numeric
codes. Here’s a detailed explanation:
Uppercase Letters (A–Z):
ASCII codes for uppercase letters range from 65 to 90.
Each uppercase letter has a unique code.
Lett ASCII Hexadeci
Binary
er Decimal mal
010000
A 65 41
01
010000
B 66 42
10
010000
C 67 43
11
010110
Z 90 5A
10
011000
a 97 61
01
011000
b 98 62
10
011000
c 99 63
11
011110
z 122 7A
10
Relationship Between Uppercase and Lowercase:
The ASCII code for a lowercase letter is exactly 32 more than its
corresponding uppercase letter.
o Example:
A → ASCII 65, a → ASCII 97 (65 + 32).
B → ASCII 66, b → ASCII 98 (66 + 32).
Conversion Formula:
1. Uppercase to Lowercase:
o Add 32 to the ASCII code of the uppercase letter.
o Example: 'A' (65) → 'a' (97) → 65 + 32 = 97.
2. Lowercase to Uppercase:
o Subtract 32 from the ASCII code of the lowercase letter.
o Example: 'a' (97) → 'A' (65) → 97 - 32 = 65.
Steps:
1. Initialize variables:
o Let I = 0 (index for traversing STR).
o Initialize an empty string UPPERSTR.
2. Traverse each character of the string:
o Repeat Steps 3 and 4 while STR[I] is not the null character ('\0').
3. Check if the character is lowercase:
o If STR[I] is in the range of ASCII values 97 to 122 (i.e., 'a' to 'z'):
Convert to uppercase by subtracting 32 from the ASCII
value.
Append the converted character to UPPERSTR.
4. Handle non-lowercase characters:
o If STR[I] is not in the range 97 to 122:
Append the character STR[I] directly to UPPERSTR.
5. Increment the index:
o Increment I to move to the next character.
6. Terminate the UPPERSTR string:
o Append a null character ('\0') to the end of UPPERSTR.
7. End:
o The UPPERSTR string now contains the uppercase version of STR.
Input:
STR = "Hello World"
Steps:
1. Initialize I = 0 and UPPERSTR = "".
2. Traverse each character of STR:
o STR[0] = 'H' → Already uppercase → UPPERSTR = "H".
o STR[1] = 'e' → Lowercase → Convert to E → UPPERSTR = "HE".
o ... (continue for all characters).
3. Append null character ('\0').
4. Output: UPPERSTR = "HELLO WORLD".
#include <stdio.h>
int main() {
char lower = 'a';
char upper = 'A';
return 0;
}
Steps:
1. Initialize Counters: Set i = 0 and j = 0.
2. Copy First String:
o While s1[i] is not the null character ('\0'):
Copy s1[i] to new_str[i].
Increment i.
3. Copy Second String:
o While s2[j] is not the null character ('\0'):
Copy s2[j] to new_str[i].
Increment i and j.
4. Terminate the New String:
o Append a null character ('\0') to new_str at position i.
5. Output: new_str now contains the concatenated result of s1 and s2.
Example Code in C
#include <stdio.h>
int main() {
char s1[] = "Hello, ";
char s2[] = "World!";
char new_str[50]; // Ensure it's large enough to hold the concatenated
string
return 0;
}
Explanation of Code:
1. String Initialization:
o s1 contains "Hello, ".
o s2 contains "World!".
o new_str is declared as an empty array to hold the concatenated
result.
2. Concatenation Process:
o Characters from s1 are copied into new_str first.
o Characters from s2 are then appended to new_str.
3. Final Termination:
o A null character ('\0') is added at the end to ensure new_str is a
valid string.
Output:
Concatenated String: Hello, World!
Steps:
1. Check String Lengths:
o If the lengths of S1 and S2 are not equal, directly conclude that
the strings are not equal and check which one is
lexicographically larger.
2. Initialize Variables:
o Set i = 0 and same = 1.
3. Character-by-Character Comparison:
o While both S1[i] and S2[i] are not null ('\0'):
If S1[i] != S2[i], set same = 0 and break the loop.
4. Check Lexicographical Order:
o If same == 0:
Compare S1[i] and S2[i]:
If S1[i] > S2[i], S1 > S2.
If S1[i] < S2[i], S1 < S2.
5. Output:
o If same == 1, the strings are equal.
o Otherwise, output which string precedes the other
lexicographically.
Example Code in C
#include <stdio.h>
int main() {
char S1[] = "apple";
char S2[] = "apples";
// Compare strings
compareStrings(S1, S2);
return 0;
}
Explanation of Code:
1. String Comparison:
o The while loop iterates through both strings simultaneously.
o Characters are compared one by one.
2. Handle Mismatches:
o If any mismatch is found, the loop exits, and same is set to 0.
3. Lexicographical Order:
o If characters differ, the code determines if S1 > S2 or S1 < S2
based on their ASCII values.
4. Handle Unequal Lengths:
o If one string ends before the other, the strings are unequal, and
the comparison is based on which one ends first.
Output Examples:
Example 1:
Input:
char S1[] = "apple";
char S2[] = "apple";
Output:
The strings are equal.
Example 2:
Input:
char S1[] = "apple";
char S2[] = "apples";
Output:
String S1 is lexicographically less than S2.
Example 3:
Input:
char S1[] = "banana";
char S2[] = "apple";
Output:
String S1 is lexicographically greater than S2.
Algorithm to Reverse a String
Input: A string S1.
Output: Reversed string S1 (in place).
Steps:
1. Initialize Pointers:
o Set i = 0 (points to the first character).
o Set j = length of S1 - 1 (points to the last character).
2. Traverse the String:
o Use a while loop that runs until i < j:
Swap the characters at positions i and j.
Increment i to move forward.
Decrement j to move backward.
3. Output:
o Once the loop completes, the string S1 is reversed in place.
Example Code in C
#include <stdio.h>
#include <string.h>
int main() {
char S1[] = "HELLO";
return 0;
}
Explanation of Code:
1. String Traversal:
o The while loop iterates until i < j. This ensures characters from
the front and back are swapped until the middle is reached.
2. Character Swap:
o Characters at positions i and j are swapped using a temporary
variable temp.
3. Update Pointers:
o Increment i to move forward.
o Decrement j to move backward.
4. Reversal Complete:
o Once the loop exits, the string is reversed.
Example:
Input:
char S1[] = "HELLO";
Output:
Reversed String: OLLEH