How to search if a string that starts with some letter in string array in C

3 Answers

0 votes
#include <stdio.h> 
 
#define ROW 5
#define COL 10
 
int startWith(const char *s, const char ch);

int main(void)
{
    char arr2d[ROW][COL] = {"c", "c++", "csharp", "java", "php"};
    
    if (startWith(arr2d[3], 'c'))
        printf("Found\n");
    else
        printf("Not Found\n"); // Not Found
        
     if (startWith(arr2d[2], 'c'))
        printf("Found\n"); // Found
    else
        printf("Not Found\n");
    
    return 0;
}

int startWith(const char *s, const char ch)
{
    if (s[0] == ch) return 1;
    
    return 0;
}


/*
run:
   
Not Found
Found

*/


answered Mar 3, 2015 by avibootz
0 votes
#include <stdio.h> 
 
#define ROW 5
#define COL 10
 
int startWith(const char *s, const char ch);

int main(void)
{
    char arr2d[ROW][COL] = {"c", "c++", "csharp", "java", "php"};
    int i;
    
    for (i = 0; i < ROW; i++)
        if (startWith(arr2d[i], 'c'))
            printf("%s\n", arr2d[i]);
    
    return 0;
}

int startWith(const char *s, const char ch)
{
    if (s[0] == ch) return 1;
    
    return 0;
}


/*
run:
   
c
c++
csharp

*/


answered Mar 4, 2015 by avibootz
0 votes
#include <stdio.h>
#include <ctype.h>

#define ROW 5
#define COL 10
 
int startWith(const char *s, const char ch);

int main(void)
{
    char arr2d[ROW][COL] = {"c", "C++", "Csharp", "java", "php"};
    int i;
    char ch = 'C';
    
    for (i = 0; i < ROW; i++)
        if (startWith(arr2d[i], tolower(ch)) || startWith(arr2d[i], toupper(ch)))
            printf("%s\n", arr2d[i]);
    
    return 0;
}

int startWith(const char *s, const char ch)
{
    if (s[0] == ch) return 1;
    
    return 0;
}


/*
run:
   
c
c++
csharp

*/


answered Mar 4, 2015 by avibootz
...