Support Forums
Random Beginner Scripts - Printable Version

+- Support Forums (https://www.supportforums.net)
+-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87)
+--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18)
+---- Forum: Programming with C++ (https://www.supportforums.net/forumdisplay.php?fid=20)
+---- Thread: Random Beginner Scripts (/showthread.php?tid=11556)



Random Beginner Scripts - Project Evolution - 08-26-2010

Screwing around with the char data-type

main.h
Code:
/*
* File:   main.h
* Author: Anthony`
*
* Created on August 26, 2010, 11:24 AM
*/

#ifndef _MAIN_H
#define    _MAIN_H

inline void toggle_case(char *ch);
bool srch_str(char *ch, char match);
inline void cptl_first(char *ch);
inline void lwrcse_first(char *ch);

#endif    /* _MAIN_H */

main.cpp
Code:
/*
* File:   main.cpp
* Author: Anthony`
*
* Created on August 25, 2010, 10:10 PM
*/

#include <iostream>
#include "main.h"
using namespace std;

/*
* lol
*/
int main() {
    char ch[10];
    int choice;

    cout << "Enter a name: ";
    gets(ch);
    cout << "\nSelect an option... 1=searchstr 2=togglecase 3=cptlzeword 4=lwrcaseword\n";
    cin >> choice;
    cout << "\n";
    switch (choice) {
        case 1:
            char matchCh;
            cout << "\n" << "Input a char you would like to search: ";
            cin >> matchCh;
            bool isFound = srch_str(ch, matchCh);
            cout << "String found: " << isFound << "\n";
            break;

        case 2:
            cout << "\n" << "Toggling individual chars ...\n";
            toggle_case(ch);
            cout << "Returned string: " << ch << "\n";
            break;

        case 3:
            cout << "Capitalizing string: " << ch << "\n";
            cptl_first(ch);
            cout << "Your capitalized string: " << ch << "\n";
            break;

        case 4:
            cout << "Lowercasing string: " << ch << "\n";
            lwrcse_first(ch);
            cout << "Your lowercased string: " << ch << "\n";
            break;

        default:
            cout << "Invalid option. Exiting ...";
    }
    return 0;
}

inline void toggle_case(char *ch) {
    for (int i = 0; ch[i]; i++) {
        if (isupper(ch[i]))
            ch[i] = tolower(ch[i]);
        else if (islower(ch[i])) {
            ch[i] = toupper(ch[i]);
        }
    }
}

bool srch_str(char *ch, char match) {
    int idx;
    for (idx = 0; ch[idx]; idx++) {
        if (match == ch[idx])
            return true;
        else
            continue;
    }

    return false;
}

inline void cptl_first(char *ch) {
    /*
     * turns off the 6th bit(to 0) resulting in a
     * capitalized char according to ASCII
     */
    *ch &= 223;
}

inline void lwrcse_first(char *ch) {
    /*
     * turns on the 1st bit(to 1) resulting in a
     * lowercased char according to ASCII
     */
    *ch |= 32;
}

More to come when im bored lol.
It would be really helpful if I received some constructive critisicm, I currently am still learning C++ and it would be helpful to me if I am given advice on using some different habits in C++.

Thanks.