Thread: Alteration of chars of a string

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    117

    Alteration of chars of a string

    So i want the user to input a string, and then reverse the capps. So "echHis" turns "into ECHhIS".

    I have no clue what to do because the book I read didn't civer this. I tried using strcmp() but it only does full strings not a character in a string i.e.: int = strcmp(str1[3],"a");

    How can I compare a specific char in a string to a given char or even a char from another string? I just want a function or a tutorial

    Thanks!
    My Ctrl+S addiction gets in the way when using Code Blocks...

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Use the == sign and single quotes around the letter you're comparing.
    Code:
    str1[3] == 'a'

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You may also want to check out the functions: isupper(), islower(), toupper(), tolower().

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    117
    Thanks oogabooga, those are exactly the functions I needed for this specific program.

    However, I see some application in the way I am approaching it (what if i am looking for a char inside of a string but don't want it to be upper/lower case).

    Here is how I written it so far. I need something along the lines of: strcpy(temp[i],abc[a]);

    Thanks!


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
        char start [50];
        int sizeStart;
    
        char abc[53] = {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"};
        char ABC[53] = {"ABCDEFGHIJKLMNOPKRSTUVWXYZabcdefghijklmnopqrstuvwxyz"};
    
        int i = 0;
        int a = 0;
    
        int compare;
    
    
        printf("Input a string to reverse the case: ");
        scanf("%s", start);
    
        sizeStart = strlen (start);
    
    printf("size of start is %d\n", sizeStart);
    
        while(i<=sizeStart)
        {
            a = 0;
            while(a<=51)
            {
                compare = strcmp(start[i],abc[a]);
    
                if(compare == 0)
                {
                    strcpy(start[i],ABC[a]);
                }
    
                a++;
    
            }
            i++;
        }
    
    
    printf("New String is: %s", start);
    
        return 0;
    }
    Last edited by JonathanS; 01-17-2012 at 07:29 PM. Reason: fix code, was messed upt a bit bc excluded not important info. same concept though.
    My Ctrl+S addiction gets in the way when using Code Blocks...

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Why (a <= 51)? Am I missing something or should it be 53?

    You're still using strcmp and strcpy, which are useless when you're working with single bytes. It's much faster and more readable if you compare the bytes with '==' and set them with ''='. That will also let you free up those extra 4 bytes you're using for "compare".

    Also, what happens if the user decides to enter a string longer than 50? It's better to use the heap for applications like this, as you can easily use much larger buffers without fears of running out of memory (50 is a plausable limit, while 2048 isn't). Make sure to free() when you're done.

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    117
    Oh, so something like this?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
        char start [50];
        int sizeStart;
    
        char abc[53] = {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"};
        char ABC[53] = {"ABCDEFGHIJKLMNOPKRSTUVWXYZabcdefghijklmnopqrstuvwxyz"};
    
        int i = 0;
        int a = 0;
    
        int compare;
    
    
        printf("Input a string to reverse the case: ");
        scanf("%s", start);
    
        sizeStart = strlen (start);
    
    printf("size of start is %d\n", sizeStart);
    
        while(i<=sizeStart)
        {
            a = 0;
            while(a<=51)
            {
                if(start[i] == abc[a])
                {
                    start[i] = ABC[a];
                }
    
                a++;
    
            }
            i++;
        }
    
    
    printf("New String is: %s", start);
    
        return 0;
    }
    I know I am missing something very basic here. Think I am doing the "start[i] = ABC[a];" wrong... Warning! Thick skull!

    EDIT: Oh, perhaps when it changes it to capps, it check for capps and changes it back to lower case. I'll need some sort of break in there, researchign it now.
    Last edited by JonathanS; 01-17-2012 at 08:18 PM.
    My Ctrl+S addiction gets in the way when using Code Blocks...

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Another thing you consider is that you could only use one char[], and add 26 when referencing the lower-case, OR having two but each of them only 1 set (both have the same thing, just different order).

  8. #8
    Registered User
    Join Date
    Sep 2011
    Posts
    117
    Didn't need some sort fo break, i needed break XD I thought break will break out of all the nested loops, not just one. Lucky me! Thanks all for your help much appreciated.

    As for the concern of the 49 length of start. Doesn't matter it is just an exercise, not a real app.

    As for the "while(a<=51)" i did that bc in my head when i set up "char abc[53]" that gives me 52 chars to work with.

    The while loop started with 0 (because as i understand the string starts at 0, so if it goes up to 51 that gives 52 chars

    also i tested it out with a string of "a-z" and it works, so I guess the computer made sense of it.
    My Ctrl+S addiction gets in the way when using Code Blocks...

  9. #9
    Registered User
    Join Date
    Apr 2011
    Location
    dust
    Posts
    70
    What about the below one?
    Code:
    int i =0;
    while(i<sizeStart)
    {
    	if((start[i]>=65)&&(start[i]<=90))
    		start[i] += 32;
    	else if((start[i]>=97)&&(start[i]<=122))
    		start[i] -= 32;
    	else
    		/* Do nothing*/
    }
    Refer the link below
    Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion

  10. #10
    Registered User
    Join Date
    Sep 2011
    Posts
    117
    Ah ASCII values, never thought of that. Great idea probably faster too! I'll have to keep that in mind when making other programs!
    My Ctrl+S addiction gets in the way when using Code Blocks...

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    The declaration of the 2 arrays abc and ABC is not good, what you do is undefined, remove the braces:
    Code:
    char abc[]="..."
    char ABC[]="..."
    And second, starting with the code from post#6, you need to add a break statement after successfully changing a character:
    Code:
    if(...==...) {
      ...
      break;
    }
    At last, replace <=51 with <sizeof(abc)-1.
    Last edited by root4; 01-18-2012 at 09:47 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Chars/String help
    By Joker719 in forum C++ Programming
    Replies: 9
    Last Post: 02-19-2011, 01:10 AM
  2. Tower of Hanoi Alteration Help!!
    By canada88 in forum C Programming
    Replies: 14
    Last Post: 12-08-2009, 06:52 PM
  3. casting a string of chars as a string of ints
    By otz111 in forum C Programming
    Replies: 5
    Last Post: 09-25-2009, 05:53 PM
  4. getting chars from a string
    By pastitprogram in forum C Programming
    Replies: 4
    Last Post: 02-18-2009, 08:36 AM
  5. Take out chars in string
    By MKirstensen in forum C Programming
    Replies: 13
    Last Post: 07-05-2008, 11:16 PM