top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

C: Search a string within the array of strings

+1 vote
568 views

Please help to write a function that searches for the unique part of a string in an array of strings.

posted Apr 22, 2015 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
You wanted to search a single string in a list of array of strings i.e
{"yes", "no", "hello", "world"} and you wanted to search "hello" i.e a single whole string,
or you wanted to search like "ll" or "es" (sub string inside a string) something like that.
please specify
Thanks
Wanted to search ll or es :)

2 Answers

+1 vote
 
Best answer

Here is the program for the above (tested). Have a look.

#include <stdio.h>
#include <string.h>

int main()
{
    char str_arr[5][10] = {"Yes", "No", "hello", "world", "test"};
    char *str = "st", *tmp = NULL;
    int i = 0;

    // It will go through each string and will check for the sub-string.
    while(i < 5) {
        if ((tmp = strstr(str_arr[i], str))) {
            /*
               If you want to ignore the case of letters i.e capital or small letter then just replace
               strstr() with strcasestr();
            */
            break;
        }
        i++;
    }

    if (tmp) {
        printf("sub string [%s] is found in string [%s]\n", str, str_arr[i]);
    } else {
        printf("string not found\n");
    }

    return 0;
}
answer Apr 27, 2015 by Arshad Khan
+1 vote

This example calls the IndexOf method on an array of strings to report the string number and index of the first occurrence of a substring. using C#

Example

string[] strArray = {"ABCDEFG", "HIJKLMNOP"};
string findThisString = "JKL";
int strNumber;
int strIndex = 0;
for (strNumber = 0; strNumber < strArray.Length; strNumber++)
{
    strIndex = strArray[strNumber].IndexOf(findThisString);
    if (strIndex >= 0)
        break;
}
Console.WriteLine("String number: {0}\nString index: {1}",
    strNumber, strIndex);

Compiling the Code

Copy the code and paste it into the Main method of a console application.

Note: here i am using Indexof method, please check this as a reference i will update the code C language as soon as possible

answer Apr 27, 2015 by Mohammed Hussain
Thx this is helpful :)
Similar Questions
+2 votes

Write a C program which accept two strings and print characters in second string which are not present in first string?

Example:
String 1: apple
String 2: aeroplane

output:
ron

+4 votes

Given a dictionary of strings and another string find out if the string is an exact match to some words in the dictionary or varies at most in only one place of some word of the dictionary?

+1 vote

char *p="India";
char t[10]="USA";

after the swap t should contain India and p as USA.

+1 vote

Consider an implementation of Strings consisting of an array where the first element of the array indicates the length of the String and the next elements represent the value of the ASCII characters in the String.
Implement String concatenation of such scenario using C?

0 votes

I am looking to reverse a string using binary search algorithm but no clue. Can someone share the algo and may be C/C++ code?

...