Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "Shorten Spaces" to "Source Code" -tkatemb #26

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
48 changes: 48 additions & 0 deletions Source Codes/Shorten Spaces.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
This removes duplicate spaces in text given from user using for loops and arrays.
Follow comments in code!
~trev
*/
#include <stdio.h>
#include <string.h>

int main(void)
{
//Declares string to store user input
char userInput[500];
//Prompt for user input
printf("Enter text (500 character limit): \n");
fgets(userInput, 500, stdin);
//Creates new string to store edited text
char newText[500];
//Declare and initialize count variable for copying characters from "userInput" to "newText"
int j = 0;
//Navigates through userInput string
for (int i = 0; i < strnlen(userInput, 500); i++)
{
//If the current index in "userInput" is a SPACE *with a character in front of it*, copy the character to "newText"
if (userInput[i] == ' ' && userInput[i + 1] != ' ')
{
newText[j] = userInput[i];
j++;
}
//If the current index in "userInput" is a NON-SPACE CHARACTER, copy the character to "newText"
else if (userInput[i] != ' ')
{
newText[j] = userInput[i];
j++;
}
//If the current index in "userInput" is a SPACE *followed by another space*, do NOT copy to "newText"
else
{
continue;
}

}
//Appends null character to the end of the "newText" string
newText[strnlen(newText, 500)] = '\0';
//Outputs the edited text
printf("Edited text: %s", newText);

return 0;
}