Like Tree1Likes
  • 1 Post By Salem

Segmentation fault? Strings

This is a discussion on Segmentation fault? Strings within the C Programming forums, part of the General Programming Boards category; This program compares two strings and outputs however many times the letter is found in the word the user inputs. ...

  1. #1
    Registered User C_Nik's Avatar
    Join Date
    Dec 2011
    Location
    Planet Earth
    Posts
    18

    Question Segmentation fault? Strings

    This program compares two strings and outputs however many times the letter is found in the word the user inputs. I'm having a problem with it getting a segmentation fault and can't figure out why? Am I somehow performing a buffer overflow with this code?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int num_appearances(char word[], char letter);
    
    
    int main()
    { 
        char wordstring[20];
        char searchLetter;
        
        printf("Please enter the word you would like to scan:\n");
        scanf("%s", &wordstring);
        
        printf("Please enter a letter to search for:\n");
        scanf("%s", &searchLetter);
        
        int num_times = num_appearances(wordstring, searchLetter);
        
        if(num_times > 0){
                     printf("The letter %s appears %d times in the word %s\n", searchLetter, num_times, wordstring);
        }
        else
            printf("The letter %s appeared zero times in your word\n");                                        
        system("PAUSE");
        return 0;
        
    }
    
    
    int num_appearances(char word[], char letter){
        int i;
        int counter = 0;
        for(i=0; i != '\0'; i++){
                 if(strcmp(&word[i], &letter)==0){
                            counter++;
                 }
                 else
                     continue;      
        }
        
        return counter;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,334
    > scanf("%s", &searchLetter);
    Yes, this is always a buffer overflow.

    If you want to read the next non-space letter, then use
    scanf(" %c", &searchLetter);

    More train-wrecks here
    Code:
        for(i=0; i != '\0'; i++){
                 if(strcmp(&word[i], &letter)==0){
    I suggest you think about it, rather than just throwing in some & all over the place to shut the compiler up.
    Lesshardtofind likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User C_Nik's Avatar
    Join Date
    Dec 2011
    Location
    Planet Earth
    Posts
    18
    Thanks thats what I love about programming. You learn something new everyday. I'll fix this pronto, but If I'm trying to compare two string characters doesn't the strcmp() function require you to pass by reference? Thats why I passed word[i] and letter that way. Thanks again

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    strcmp() is for comparing two strings, but in your for-loop I think you want to compare two characters. For comparing two characters you just use the "==" operator.

    Bye, Andreas

  5. #5
    Registered User C_Nik's Avatar
    Join Date
    Dec 2011
    Location
    Planet Earth
    Posts
    18
    Ok Thanks I appreciate that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault (strings, pointers, Linux)
    By ankushwrites in forum C Programming
    Replies: 2
    Last Post: 03-25-2012, 12:00 PM
  2. Unable to join two strings due to Segmentation Fault
    By Orange Lozenge in forum C++ Programming
    Replies: 3
    Last Post: 09-15-2011, 04:00 PM
  3. Segmentation fault when appending to strings (char *)
    By LanguidLegend in forum C Programming
    Replies: 15
    Last Post: 02-24-2011, 06:35 PM
  4. Searching and matching strings: segmentation fault
    By Smola in forum C Programming
    Replies: 18
    Last Post: 07-11-2005, 12:25 AM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 10:09 PM

Tags for this Thread


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