0

I'm trying to use a for loop to go through two strings character by character and see where they are equal, where they are different, and if so if both are letters than I do one condition, but if one is a letter and one is an underscore, then I do something else. The first part of the loop works fine; it can tell when the two values are equal. However, when I use isalpha to see if they are both different letters, then it doesn't do it.

#include <stdio.h>
#include <string.h>
#include <ctype.h>
char s1[] = "v_intner_";
char s2[] = "wri_t_ers";


char* getTranscript(char* s1, char* s2){
    int i;
    char sOneTemp[9];
    char sTwoTemp[9];
    char eTranscript[9];
    strcpy(sOneTemp, s1);
    strcpy(sTwoTemp, s2);

    for(i=0; i<9; i++){
        if(sOneTemp[i]==sTwoTemp[i]){
            printf("Those two characters are equal.\n");
        }
        if(sOneTemp[i]!=sTwoTemp[i]){


            if(isalpha(sOneTemp[i]) && isalpha(sTwoTemp[i])){
                printf("Both are letters\n");
            }
            else{
                printf("One is a dash\n");
            }
        }
    }
printf("The value of s1 is: %s\n", sOneTemp);

return s1;}
main()
{
printf("The main method returns: %s", getTranscript(s1,s2));
return 0;
}
  • 3
    Step through your code with a debugger. Or compile a tiny example that proves that `isalpha` is broken (hint: it isn't). It drives people nuts when beginners try to program by writing giant complex algorithms, and then posting their entire code to Stack Overflow saying "it doesn't work". Learn to break the problem into easy-to-solve pieces, and/or learn to use a debugger. – Jonathon Reinhart Mar 02 '15 at 04:40
  • I know that isalpha isn't broken because I've tested it with a single example like: char ch = 'a'; if(isalpha(ch)){printf("it's a character");} – Tom Rodgers Mar 02 '15 at 04:41
  • Consider to install `libasan` and compile with `-g -fsanitize=address`. This needs at least gcc 4.8. It helps to find buffer overflows. Output for your program: `==1842==ERROR: AddressSanitizer: stack-buffer-overflow on address [...] WRITE of size 10 at [...] thread T0 #0 [...] in strcpy (/lib64/libasan.so.1+0x30203) [...] Address 0x7fffdbbf48d9 is located in stack of thread T0 at offset 41 in frame #0 0x400a75 in getTranscript (/home/osboxes/a.out+0x400a75) This frame has 2 object(s): [32, 41) 'sOneTemp' <== Memory access at offset 41 overflows this variable`[...] – 4566976 Mar 03 '15 at 16:39

3 Answers3

2

Your isalpha logic is correct. However you have a buffer overflow:

char sOneTemp[9];
char sTwoTemp[9];

but you then use strcpy to copy 10 characters in (remember that C strings have a null terminator). If you fix this error then the code gives the expected output.

A good fix would be to just use s1 and s2 inside your function instead of redundantly copying the input strings.

M.M
  • 138,810
  • 21
  • 208
  • 365
  • NB: I hope you aren't planning to return `eTranscript` – M.M Mar 02 '15 at 04:44
  • Yes I was planning on returning eTranscript? Why? – Tom Rodgers Mar 02 '15 at 04:49
  • @TomRodgers it's a local variable to the function, so it will be destroyed when the function returns. [See here](http://stackoverflow.com/questions/25798977/returning-string-from-c-function) for some ideas. – M.M Mar 02 '15 at 04:52
  • @TomRodgers `eTranscript ` has scope limited to function `getTranscript` only. – Vagish Mar 02 '15 at 04:52
2

Your program is subject to undefined behavior. To use strcpy the string "v_intner_", the target needs at least 10 characters. You have created arrays that have 9 characters only.

Change the lines:

char sOneTemp[9];
char sTwoTemp[9];

to

char sOneTemp[N]; // Where N is 10 or greater.
char sTwoTemp[N];
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Thank you. I miscounted my strings. Jeez, I wasted an hour trying to figure that out. LOL! Wish I could upvote you but I don't have enough reputation. Hopefully others will. – Tom Rodgers Mar 02 '15 at 04:48
0
char sOneTemp[9];
char sTwoTemp[9];
char eTranscript[9];
strcpy(sOneTemp, s1);
strcpy(sTwoTemp, s2);

Your array-size is not enough to hold the 10 characters including the nul character. So increase your array size to include the nul character

Gopi
  • 19,784
  • 4
  • 24
  • 36