简体   繁体   中英

Inputting, Searching and Deleting String in an Array in C

Is it possible to input a string into any place in a two dimensional array?

char courseName[8][50] = {NULL};

printf("Enter your second course: ");
scanf("%[^\n]s", courseName[2]);

printf("Enter your fourth course: ");
scanf(" %[^\n]s", courseName[4]);

for(int i = 1; i < 9; i++)
{
    printf("Name of the Courses in courseName[i][50] is %s\n", courseName[i]);
}

or delete a string inside the array by typing the name of the string?

char deleteCourse[50];

printf("Enter the course name to delete the course");
scanf(" %[^\n]s", deleteCourse);

for(int i = 1; i < 9; i++)
{
  if(deleteCourse == courseName[i])
    {
     courseName[i] = {NULL};
    }
 printf("Your edited courses: %s\n", courseName[i]);
}

The best way to go about this would be to use pointers since you could simply strcpy to your destination of choice using pointer arithmetic. I'm not going to explain pointers here, I'm just going to show you a path you might take to do what you want to do.

What I'm about to show you is bare bones code that you can apply and modify according to your needs. It by no means takes all possible error cases into account.

Place an input string into destination of choice

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

int main(int argc, char *argv[]){

    /* Variables */
    char **courseName;
    int i;

    /* Allocate memory to make courseName look like: courseName[4][100] */ 
    /* I assume the memory allocated properly, but you need to check that */
    courseName = malloc(sizeof(char*) * 4);
    for(i = 0; i < 4; i++){
        courseName[i] = malloc(100);
    }

    /* Copy string to your destination of choice using pointer arithmatic */
    strcpy(courseName[2] + 50, "word");

    /* Printing at different locations to show results */
    printf("index [1][0]   = %s\n", courseName[1]);
    printf("index [2][0]   = %s\n", courseName[2]);
    printf("index [2][50]  = %s\n", courseName[2] + 50);
    printf("index [2][51]  = %s\n", courseName[2] + 51);

    return 0;
}

Output:

index [1][0]   = 
index [2][0]   = 
index [2][50]  = word
index [2][51]  = ord

Deleting a string from within a two dimensional array

Note: the code below does assume that word is at index [i][0] where i is the array in which the word is found. Since you don't specify exactly what you need, I'm assuming, from reading your question, that this should be ok. Nonetheless, you can modify according to your needs.

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

int main(int argc, char *argv[]){

    /* Variables  */
    char **courseName, deleteWord[10], *result;
    int i, arrayNum;

    /* Allocate memory to make courseName look like: courseName[4][100] */
    courseName = malloc(sizeof(char*) * 4);
    for(i = 0; i < 4; i++){
        courseName[i] = malloc(100);
    }

    /* Place a word into the array at a random location for the sake of the example */
    strcpy(courseName[2], "word");
    printf("index [2][0]  = %s\n", courseName[2]);

    /* The worrd we want to delete (can be a user input if you want) */
    strcpy(deleteWord, "word");

    /* Loop through the different arrays */
    for(i = 0; i < 4; i++){
        if( (result = strstr(courseName[i], deleteWord)) != NULL ){ /* use strstr() see if the word want to delete exists */
            arrayNum = i; /* Get the array that the word is in (in this case courseName[2]) */
        }
    }

    /* Empty the array at the location where the word is */
    memset(courseName[arrayNum], 0, strlen(deleteWord));

    /* "word" is deleted */
    printf("index [2][0]  = %s\n", courseName[2]);

    /* but "sss" still exists */
    printf("index [2][4]  = %s\n", courseName[2] + 4);
    return 0;
}

Output:

index [2][0]  = wordsss
index [2][0]  = 
index [2][4]  = sss

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM