I have two strings and want to compare them, but between two variables. Example: String a = "stackoverflow" and String b = "stacknotoverflow". I want to check if every char between the 5th position of String a (int = 5 till int =9) and 8th position of String b (int = 8 till int = 12) position of both strings is the same. Can anyone help me with this simple problem?
            Asked
            
        
        
            Active
            
        
            Viewed 493 times
        
    -9
            
            
        - 
                    1C or C#? Why both tags? – Some programmer dude Nov 08 '18 at 15:00
- 
                    1What have you tried doing and where is the problem? – UnholySheep Nov 08 '18 at 15:00
- 
                    I have an array char a[1000][100]. And i am getting back a string:. Now i want to compare them, but only the positions between the 5th and 8th char. – chilla77 Nov 08 '18 at 15:05
- 
                    1Reopening. This question is about comparing internal substrings, not a simple string comparison. – dbush Nov 08 '18 at 15:24
- 
                    Voting to close as "too broad" again. OP did not show any attempt. – Jongware Nov 08 '18 at 15:38
- 
                    Ahh ok so the question is not right?? – chilla77 Nov 09 '18 at 15:17
- 
                    And sorry for not being online 24/7 after the question was solved got some IRL stuff as well – chilla77 Nov 09 '18 at 15:19
2 Answers
2
            You can use the strncmp function to compare two strings up to a maximum number of characters.  To start a comparison in the middle of a string, you would pass in the address of the array element to start the comparison at.
For example:
if (strncmp(&string1[4], &string2[4], 4) == 0) {
    printf("characters 5 - 8 are the same\n");
} else {
    printf("characters 5 - 8 are not the same\n");
}
 
    
    
        dbush
        
- 205,898
- 23
- 218
- 273
-1
            
            
        Assuming you're talking about C.
https://linux.die.net/man/3/strcmp
This function is used to compare strings in C.
int result = strcmp(str1, str2);
printf("Compare result: %d", result);
 
    
    
        Someone
        
- 21
- 1
