Thread: Need some help Replacing the letters in a string to uppercase.

  1. #1
    Registered User
    Join Date
    Jun 2016

    Unhappy Need some help Replacing the letters in a string to uppercase.

    I had to make code where certain letters of upper case has to change to numbers.

    A - 4
    B - 8
    E - 3
    I - 1
    O - 0
    S - 5

    So, for example it would be like this :
    This stuff really works ----> TH15 5TUFF R34LLY W0RK5

    And that's the code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <stdlib.h>
     const char* TABLE[] = { "ABEIOS", "483105" };
    int main(void) {
        char *a;
        char str[1000], *ptr;
        printf("%s\n", "enter the string:");
        if(scanf("%s", &*a) != 1) {
           printf("input error\n");
         for ( ptr = str; ptr; ++ptr ) {
            *ptr = toupper(*ptr);
         if (( a = strchr(TABLE[0], *ptr) ) != NULL );
           *ptr = (TABLE[1][a - TABLE[0]]);
        printf("%s\n", str);
    And then i trying to compile this , i get "Segmentation fault".

    How to fix it ?


  2. #2
    Registered User
    Join Date
    Jun 2015
    &*a is the same as a.
    Using scanf with "%s" won't allow you to read a string with spaces.
    To read a string with spaces, use scanf with "%999[^\n]" or, far better, use fgets.
    Why are you using a in your scanf? Shouldn't you be using str?
    Otherwise, your algorithm looks correct.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > if (( a = strchr(TABLE[0], *ptr) ) != NULL );
    Watch the ; at the end of the line.

    You've written this.
    if (( a = strchr(TABLE[0], *ptr) ) != NULL ) {
      // do nothing
    // a could be NULL here
    *ptr = (TABLE[1][a - TABLE[0]]);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-11-2013, 11:12 PM
  2. converting a string array into uppercase letters from file
    By thesyntaxdr in forum C Programming
    Replies: 6
    Last Post: 04-20-2013, 08:37 AM
  3. Replacing letters in string with numbers.
    By m.truman.88 in forum C Programming
    Replies: 4
    Last Post: 12-20-2011, 01:19 PM
  4. Counting uppercase and lowercase letters in a text
    By Tintu in forum C Programming
    Replies: 2
    Last Post: 02-06-2008, 10:15 PM

Tags for this Thread