Some string manipulations I've been working on (Language: C)
- Reversing a String
- Removing Spaces from a String
- Reversing Words in a String
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 100
/**
* String Manipulation Exercises
*
* @author Kevin Leong
* @date January 11, 2011
*/
int main (char argc, char* argv[]) {
// Initialize string to hold user input
char myString[MAX_LEN] = {0};
// Prompt for user input
printf("Please enter a string (100 character max: ");
// Read in user input
if (fgets(myString, MAX_LEN, stdin) == NULL) {
perror("Error reading user input");
}
// If the last character of the string is a new line
// character, then remove it from the string.
int last = strlen(myString) - 1;
if (myString[last] == '\n') {
myString[last] = '\0';
}
// Copy user entered string to allow the functions to
// independently manipulate it
int len = strlen(myString);
char myStringReverse[len];
char myStringRemoveSpaces[len];
char myStringReverseWords[len];
strcpy(myStringReverse, myString);
strcpy(myStringRemoveSpaces, myString);
strcpy(myStringReverseWords, myString);
// Reverse the characters in place
reverse_string(myStringReverse);
remove_spaces(myStringRemoveSpaces);
reverse_words(myStringReverseWords);
// Print manipulated strings
puts(myStringReverse);
puts(myStringRemoveSpaces);
puts(myStringReverseWords);
} // End Main
/**
* Reverses a string in place.
*
* @param s - string to reverse
*/
void reverse_string (char s[]) {
// Get string length
int len = strlen(s);
// If string is of length 0 or 1, then no reversing necessary
if (len > 1) {
// Loop variable
int i;
// Temporary placeholder for swapped elements
char temp;
// Go through string and swap characters
for (i = 0; i < (len / 2); i++) {
// End is equally distant from the end of the string
// as i is from the beginning
int end = len - 1 - i;
// Swap characters at i and end indexes
temp = s[i];
s[i] = s[end];
s[end] = temp;
} // End For loop
} // End If
} // End Reverse String Method
/**
* Removes spaces from a given string.
*
* @param s - string to remove spaces from
*/
void remove_spaces (char * s) {
// Counter to track number of spaces so far
int numSpaces = 0;
// String length
int len = strlen(s);
// Loop variable
int i;
// Loop thru string
for (i = 0; i <= len; i++) {
// If the current position is a space, then increment
// space counter
if (s[i] == ' ') {
numSpaces++;
}
// If current position is not a space and there exist
// spaces in the string
else if (numSpaces != 0) {
// Move the element to the left. The amount of positions
// it's moved is dependent on how many spaces have been
// discovered.
s[i - numSpaces] = s[i];
}
} // End For Loop
} // End Remove Spaces Method
/**
* Reverses the words in a string.
*
* @param s - string to manipulate
*/
void reverse_words (char * s) {
// Length of String
int stringLen = strlen(s);
// Loop index
int i;
// Index to track progress
int k = stringLen;
// New string holding reversed words
char newString[stringLen];
newString[stringLen] = '\0';
// Loop through string and replace all spaces with terminating 0s
for (i = 0; i < stringLen; i++) {
if (s[i] == ' ') {
s[i] = '\0';
}
} // End For Loop
// Length of current word
int wordLen;
// Restart loop variable
i = 0;
// Reverse the order of the words
while (i < stringLen) {
// Length of current word
wordLen = strlen(&s[i]);
// Go backwards on new string
k -= wordLen;
// Loop variable
int j;
// Copy word into new string
for (j = 0; j < wordLen; j++ ) {
newString[k + j] = s[i + j];
}
// Go forward on original string
i += wordLen;
// If there's a terminating zero and it's not the end
// of the string, then put a space in the new string
while ((i < stringLen) && (s[i] == '\0')) {
i++;
k--;
newString[k] = ' ';
} // End While Loop
} // End While Loop
// Copy manipulated string back to original string
strcpy(s, newString);
} // End Reverse Words Method
No comments:
Post a Comment