String manipulation

This is a discussion on String manipulation within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <string.h> //Define all Variables char string[100]; int count; // Prompt User to Enter String int array[27]= ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    8

    String manipulation

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    //Define all Variables
    char string[100];
    int count;      
    
    
    // Prompt User to Enter String
    
    
    int array[27]= {0b0001,0b001111,0b10110,0b00000,0b011,0b11011,0b001100,0b1001,0b0100,
                    0b11001011,0b1100100,0b00001,0b11000,0b0101,0b0010,0b001110,0b1100101000,0b1010,
                    0b1000,0b111,0b10111,0b110011,0b11010,0b1100101001,0b001101,0b1100101010,0b1100101011};
    int main() {
    
    
    do{
    
    
    printf("Enter String: ");
    scanf("%s", string);
    
    
    for(count=0; count<=strlen(string); count++) { 
       if(string[count]>97 && string[count] >= 122)
          string[count]=string[count]-32;
    }
    
    
    printf(" String to Encode: %s", string);
    
    
    }
    while(strcmp(string, "quit")); 
    }

    Hey guys,


    I wrote the following code so far, which asks the user to input a string less than 100 characters such as "Programming's Hard!"


    I am trying to get it so that the program will be able to restate the string that the user entered above, but do three things differently:
    1) switch it to all capital letters if its not already in capital,
    2) remove any spaces that may be in the string
    3) remove any no alphabetic symbols (i.e. '!')


    the output should look like this:


    Enter String: Programming's Hard!


    String to Encode: PROGRAMMINGSHARD






    If you guys could help me understand how to do any of the three things, i would really appreciate it, i am really lost as to how to go about it.


    thanks!

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    You can do this in-place or creating a new string, whichever you require. I'll give you a hint - use 2 index counters or 2 pointers.

    Think about how you would transform

    |P|r|o|g|r|a|m|m|i|n|g|'|s| |H|a|r|d|!|\0| into

    |P|R|O|G|R|A|M|M|I|N|G|S|H|A|R|D|\0| by examination.

    3 functions to help you are isalpha(), isspace(), and toupper(). Unless you're looking to reinvent the wheel.

  3. #3
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    289
    I would complete the assignment using the functions listed by Tclausex. But when you have more time, try to replace the functions with your own working versions. This will help build your skills, and those three aren't that hard to reproduce. At least rudimentary versions.

    And what is the large array, so descriptively named array, used for?
    Last edited by jwroblewski44; 02-17-2013 at 08:11 PM.

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    8

    A general outline on what to do....

    Quote Originally Posted by amitj93 View Post
    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    //Define all Variables
    char string[100];
    int count;      
    
    
    // Prompt User to Enter String
    
    
    int array[27]= {0b0001,0b001111,0b10110,0b00000,0b011,0b11011,0b001100,0b1001,0b0100,
                    0b11001011,0b1100100,0b00001,0b11000,0b0101,0b0010,0b001110,0b1100101000,0b1010,
                    0b1000,0b111,0b10111,0b110011,0b11010,0b1100101001,0b001101,0b1100101010,0b1100101011};
    int main() {
    
    
    do{
    
    
    printf("Enter String: ");
    scanf("%s", string);
    
    
    for(count=0; count<=strlen(string); count++) { 
       if(string[count]>97 && string[count] >= 122)
          string[count]=string[count]-32;
    }
    
    
    printf(" String to Encode: %s", string);
    
    
    }
    while(strcmp(string, "quit")); 
    }

    Hey guys,


    I wrote the following code so far, which asks the user to input a string less than 100 characters such as "Programming's Hard!"


    I am trying to get it so that the program will be able to restate the string that the user entered above, but do three things differently:
    1) switch it to all capital letters if its not already in capital,
    2) remove any spaces that may be in the string
    3) remove any no alphabetic symbols (i.e. '!')


    the output should look like this:


    Enter String: Programming's Hard!


    String to Encode: PROGRAMMINGSHARD






    If you guys could help me understand how to do any of the three things, i would really appreciate it, i am really lost as to how to go about it.


    thanks!

    create an array of the same size to hold the new string (since we know its length
    will be <= the original (use strlen). Or you could just copy the characters in
    order back into the original string....

    starting at the first character in the string...

    for all characters

    Make the character lower case by AND'ing with ~0x20.

    (For instance, this converts 'a' to 'A', since 'a' is 0x61 = 0b01100001 and
    'A' is 0x41 = 0b01000001. Bitwise AND'ing ('&') with ~0x20 = 0b11011111 = 0xDF
    will clear the bit that needs be turned from a 1 to a 0.)

    If the character is alphabetic (use isalpha, or just ask is ( ch >= 'A' &&
    ch <= 'Z' ), since you just turned lower case to upper case by AND'ing, you
    don't need to test for >= 'a' and <= 'z'....)

    then copy it to the next position in the output string (or the
    original string, if you prefer, and increment the index into the new
    string

    (Notice that you don't do anything to the other characters since they're
    not alphabetic, so don't increment your counter into the new string for
    these characters or copy them to the new string.

    End for

    Terminate the new string with a 0x00 value.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    Please be aware that no such syntax exists for binary constants in C. You are using a C Extension provided by GCC.
    Of course you're not even using that array at present, so what is it even for?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String manipulation
    By gadu in forum C Programming
    Replies: 3
    Last Post: 08-29-2008, 02:42 AM
  2. string vector to string pointer manipulation
    By stanlvw in forum C++ Programming
    Replies: 11
    Last Post: 07-16-2008, 01:43 AM
  3. String manipulation
    By Raghavan in forum C Programming
    Replies: 2
    Last Post: 01-15-2008, 09:24 PM
  4. string manipulation
    By knj316 in forum C++ Programming
    Replies: 3
    Last Post: 05-31-2005, 11:36 PM
  5. string manipulation
    By ubershatten in forum C++ Programming
    Replies: 1
    Last Post: 07-25-2002, 10:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21