Wednesday, January 22, 2014

String Rearrange in C

This was a little extra credit thing I wrote in C for a class. Description is after include statements.


//Hailey Hanson
#include<stdio.h>
#include<string.h>
//This code will take a string and give back the same string, reversed, without anything but letters and with upper and lower case letters flipped.

int main(){
char array[1024];
char letter_array[1024];
int i;

fputs("Enter a string\n", stdout);
fgets(array, 1024, stdin);      //get string from user

for (i=strlen(array); i>=0; i--){        //walk through string backwards
        if (isalpha(array[i])){         //take out non-letters
                 letter_array[i]=array[i];
                if (isupper(array[i])){ //change upper to lower
                        array[i] = tolower(array[i]);
                        letter_array[i]=array[i];
                }
                else if (islower(array[i])){    //change lower to upper
                        array[i] = toupper(array[i]);
                        letter_array[i]=array[i];
                }
                fprintf(stdout, "%c", letter_array[i]);
                }//end of if

}//end of for

fprintf(stdout, "\n");

}//end of main


No comments:

Post a Comment