Thread: String Array

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    4

    String Array

    Hello, I have a question about my code. I have been writing code for not more than a month now so all of this is kinda new to me.
    I want to make a program asks for a letter (saves it in removedChar), asks me to write a text, and prints out the text without my the letter in it. I want to use arrays (I tried with two arrays) and fgets().

    Code:
    case 4:
    
    system("cls");
    printf("Enter what character u wish to remove\n\n");
    
    
    removedChar = getchar();   // saves my char in removedChar
    getchar();
    
    
    printf("Your char is %c:\nEnter a text:\n\n", removedChar);
    
    
    char str1[50], str2[50];       // creating two arrays
    
    
    fgets(str1, 50, stdin);          // Typing in my text
    
    
    strcpy(str2, str1);               // copying str1 into str2.
    
    
    int i, j, n;
    n = strlen(str1);
    for (i = 0; i<n; i++);
    {
    if (str1[i] != removedChar)
    {
    str2[j] = str1[i];
    j++;
    }
                    
    }
    printf("your text is:\n%s", str2);*/
                
    break;
    Last edited by orgee; 12-09-2016 at 07:03 AM.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You should create a complete simplified version of the code to post. Also be sure to use proper indentation.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
        int removedChar;
    
        printf("Enter what character you wish to remove\n\n");
    
        removedChar = getchar();
        getchar();
    
        printf("Your char is %c:\nEnter a text:\n\n", removedChar);
    
        char str1[50], str2[50];
    
        fgets(str1, 50, stdin);
    
        strcpy(str2, str1);
    
        int i, j, n;
        n = strlen(str1);
    
        for (i = 0; i<n; i++);
        {
            if (str1[i] != removedChar)
            {
                str2[j] = str1[i];
                j++;
            }
        }
    
        printf("your text is:\n%s", str2);
    
        return 0;
    }
    Make sure you heed the warnings:

    Code:
    main.c|28|warning: 'j' may be used uninitialized in this function
    You have a stray semi-colon after the "for" which needs to be removed. As it is, your code is actually doing this:

    Code:
    for (i = 0; i<n; i++)
        ;
    
    {
        if (str1[i] != removedChar)
        {
            str2[j] = str1[i];
            j++;
        }
    }
    There's no need to copy str1 into str2 if you're just going to be writing to str2 one element at a time.

    Also be sure str2 is properly terminated.

  3. #3
    Registered User
    Join Date
    Dec 2016
    Posts
    4
    Yeah sorry about that. here it is corrected. code breaks when I try to enter the loop, the copy (strcpy) part was actually for me to see if it copys (removed the printf after it), but I couldn't actually get there.

    Code:
    int main()
    {
             char str1[50], str2[50], removedChar;
    
             removedChar = getchar();  
             getchar();
    
    
             printf("Your char is %c:\nEnter a text:\n\n", removedChar);
    
    
             fgets(str2, 50, stdin);
    
    
             int i, j = 0, n;
    
    
             n = strlen(str2);
             for (i = 0, j; i<n; i++)
                   {
                          if (str2[j] != removedChar)
                          {
                                 str1[i] = str2[j];
                                 i++;
                           }                
                   }
              str1[i] = '\0';
              printf("your text is:\n%s", str1);
    
    return 0;
    }
    Last edited by orgee; 12-09-2016 at 08:36 AM.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Edit: Removed comment as code was updated.

    How does the code "break"?
    Last edited by Matticus; 12-09-2016 at 08:17 AM.

  5. #5
    Registered User
    Join Date
    Dec 2016
    Posts
    4
    Im using Visual Studio, either code is executing or entering break mode, dont know what it means exactly.
    After a PC restart the code works, problem gets after fgets(), right after I write something the program exits.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    It sounds like the console window is just closing when the program completes, meaning you can't see the output.

    See top answer here: Preventing console window from closing on Visual Studio C/C++ Console application - Stack Overflow

  7. #7
    Registered User
    Join Date
    Dec 2016
    Posts
    4
    The "break" is not really the problem, Im getting it once in a while just have to "Build" ->"Rebuild Solution".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding a string array to another string array?
    By vertigodown in forum C Programming
    Replies: 1
    Last Post: 04-20-2012, 09:56 AM
  2. Replies: 3
    Last Post: 06-10-2011, 07:47 PM
  3. Converting tchar array to string array
    By GeekInTraining in forum C++ Programming
    Replies: 2
    Last Post: 01-17-2011, 01:34 AM
  4. Replies: 1
    Last Post: 12-10-2008, 11:29 AM
  5. convert char** (c string array) to std::string[]
    By umen242 in forum C++ Programming
    Replies: 2
    Last Post: 11-11-2008, 05:52 AM

Tags for this Thread