Thread: Print after period program

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    8

    Print after period program

    I am trying to make a program that will take a file name and print the characters after the period. Ex: "text.cpp" will be turned into "cpp" The program is sort of working. I am not sure how to make the program print more than one character. All it does is print the first character and if there is more than one character after the period it doesn't print it. Any help is appreciated.

    Code:
    #include <stdio.h>
    
    int main()
    {
            char fname[50];
            int x=0;
    
            printf("Enter filename: ");
            scanf("%s",fname);
    
            while(x < strlen(fname))
            {
              x++;
    
                    if(fname[x] == '.')
                    {
                      x++;
                      printf("%c", fname[x]);
                      return 0;
                    }
    
            printf("Error: invalid entry!");
    
            return 0;
    
     }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    %s is for printing a string, but then you'll need to provide it a pointer, not a character.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ejiroy View Post
    I am trying to make a program that will take a file name and print the characters after the period. Ex: "text.cpp" will be turned into "cpp"
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main( void )
    {
            char fname[50];
    char *ext;
    
            printf("Enter filename: ");
            scanf("%49s",fname);
    
    printf( "\n");
    
    ext = strchr(fname, '.');
    if (! ext)
      printf("Invalid filename or missing extension");
    else
      printf("Extension is : %s",ext + 1);  // remove "+ 1" to keep the dot
    
    return 0; 
    }

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    8
    thanks for the help, I found the problem already.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program unable to print anything
    By SasDutta in forum C Programming
    Replies: 7
    Last Post: 07-23-2010, 10:04 AM
  2. c program that accepts and executes commands?
    By Cimposter in forum C Programming
    Replies: 3
    Last Post: 09-30-2009, 02:58 PM
  3. Replies: 2
    Last Post: 09-16-2009, 06:00 AM
  4. Why is it that my program does not print for case 1?
    By Jasper in forum C Programming
    Replies: 10
    Last Post: 07-02-2009, 12:57 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM