Created
September 21, 2017 16:57
-
-
Save FeraruSilviuMarian/cf35f9f64d4f9a385356ca0a9aed489d to your computer and use it in GitHub Desktop.
String to Acronym
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <cs50.h> | |
#include <string.h> | |
#include <ctype.h> | |
// takes any string as input, returns the first letter (to uppercase) of each word as output | |
string StrToAcronym (string text); | |
int main (void) | |
{ | |
// input string from user | |
string name = get_string(); | |
// printing the output of the function with the user input as argument | |
printf("%s\n", StrToAcronym(name) ); | |
} | |
// making everything into a general function | |
string StrToAcronym (string text) | |
{ | |
int ci = 0; // input interator (text) | |
int ii = 0; // intermediate iterator (initials array) | |
// intermediate char array, stores first letters of each word found | |
int iniLen = 40; // the size of the array that stores first letters | |
char initials[iniLen]; // array that stores first letters | |
// filling intermediate array with spaces to clean it from junk memory | |
for (int i = 0; i < iniLen; i++) | |
{ | |
initials[i] = ' '; | |
} | |
bool wordFound = false; | |
// iterating through the string until the end of the string | |
while (text[ci] != '\0') | |
{ | |
// if I didn't find a word and I'm looking at a space, advance to the next character | |
if (wordFound == false && text[ci] == ' ') | |
{ | |
ci++; | |
} | |
// if I didn't find a word and I'm looking at a character, I found a word | |
if (wordFound == false && text[ci] != ' ') | |
{ | |
wordFound = true; | |
initials[ii] = text[ci]; // saving the first character of the word I found | |
ii++; // advancing intermediate char array iterator (initials iterator) | |
} | |
// if I found a word and I'm not looking at a space, advance | |
if (wordFound == true && text[ci] != ' ') | |
{ | |
ci++; | |
} | |
// if I found a word and I'm looking at a space, word ended, looking for a new word | |
if (wordFound == true && text[ci] == ' ') | |
{ | |
wordFound = false; | |
} | |
} | |
// changing char array to uppercase and getting rid of unwanted characters | |
for (int i = 0; i < iniLen; i++) | |
{ | |
if ( initials[i] != ' ' && initials[i] != '\x00' ) | |
{ | |
initials[i] = toupper(initials[i]); | |
} | |
} | |
// determining size of initials (without spaces) | |
int inSize = 0; | |
int wi = 0; | |
while (initials[wi] != ' ') | |
{ | |
inSize++; | |
wi++; | |
} | |
// making a new array of the propper size and copying the contents to it | |
char newAr[inSize]; | |
for (int i = 0; i < inSize; i++) | |
{ | |
newAr[i] = initials[i]; | |
} | |
// converting from char array to string to able to return it as string | |
string outstr = newAr; | |
return outstr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment