Thread: Reversing String from .txt ignoring special characters

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    15

    Reversing String from .txt ignoring special characters

    The .txt file says "The Homework is due on Friday. Good$?Luck!"

    Console:
    The Homework 1 is due on Friday. Good$?Luck!
    ehT krowemoH 1 si eud no .yadirF !kcuL?$dooG
    It should be like this: ehT krowemoH si eud no yadirF. dooG$?kcuL!
    I'm trying to get the program to ignore special characters, but am struggling as to where to add an if statement.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include<stdlib.h>
    
    void reverse_string(char*);
    void reverse_words(char*);
    
    int main()
    {
    
    FILE *fp;
    char Input[255];
    fp = fopen("test.txt", "r");
    fgets(Input, 255, (FILE*)fp);
    printf("%s\n", Input);
    reverse_words(Input);
    printf("%s", Input);
    
      return 0;
    }
    
    void reverse_words(char *s) {
      char b[100], *t, *z;
      int c = 0;
    
      t = s;
    
      while(*t) {                           //processing complete string
        while(*t != ' ' && *t != '\0') {    //extracting word from string
          b[c] = *t;
          t++;
          c++;
        }
        b[c] = '\0';
        c = 0;
    
        reverse_string(b);        // reverse the extracted word
    
        z = b;
    
        while (*z) {    //copying the reversed word into original string
          *s = *z;
          z++;
          s++;
        }
         if (*s == ' ') //Skipping spaces
            {
                s++;
            }
        t = s;                              // pointing to next word
    }
    }
    
    void reverse_string(char *t) {
      int l, c;
      char *e, s;
      l = strlen(t);
      e = t + l - 1;
      for (c = 0; c < l/2; c++) {
        s  = *t;
        *t = *e;
        *e = s;
        t++;
        e--;
        }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,603
    > fgets(Input, 255, (FILE*)fp);
    1. Remove the cast on the fp. It is totally not necessary.

    > but am struggling as to where to add an if statement.
    Basically everywhere you already compare with space.
    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.

  3. #3
    Registered User
    Join Date
    Sep 2018
    Posts
    15
    while(*t != ' '&& *t != '\0')
    if(*s == ' ')

    I tried adding *s/t || '$' to these lines, but that didnt seem to work

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparing special characters/Ignoring escape sequences
    By Tigertan in forum C Programming
    Replies: 1
    Last Post: 11-12-2017, 12:28 PM
  2. Comparing special characters/Ignoring escape sequences
    By Tigertan in forum C Programming
    Replies: 2
    Last Post: 11-12-2017, 08:24 AM
  3. Ignoring specific characters in a string
    By seanksg in forum C Programming
    Replies: 11
    Last Post: 05-02-2011, 06:08 PM
  4. String and special characters (*)
    By TuXaKoS in forum C Programming
    Replies: 7
    Last Post: 11-02-2010, 04:24 AM
  5. Placing special characters inside a string
    By forum member in forum C Programming
    Replies: 3
    Last Post: 12-07-2007, 08:43 AM

Tags for this Thread