Thread: C Programming Arrays and Strings

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    8

    C Programming Arrays and Strings

    So I'm creating a program that produces a random string, then allows the user to enter a string, followed by a symbol. The random string is then compared to the entered string and the symbol string replaces when the chars are equal. This is a skeleton so far (Trying to get the basic components working before I do error checks) and I'm simply not doing the swap correctly. I thought that going by individual memory indices and comparing those then placing the string when equal would be the way but after taking the symbol the program just exits out. This is also done with just array notation Any suggestions would be greatly appreciated.
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    char *rando(char rarray[]);
    void *generated(char string1[]);
    char *symbol(char symb[], char rarray[], char string1[], char Farray[]);
    
    
    int main()
    {
        char s1[512] = { 0 };
        char userinput[40] = { 0 };
        char symbol1[40] = { 0};
        char finished_string[512] = { 0 };
        printf("%s\n", rando(s1));
        generated(userinput);
        printf("%s", symbol(symbol1, s1, userinput, finished_string));
     getch();
     return 0;
    }
    
    
    char *rando(char rarray[])
    {
        int counter  = 0;
        int i = 0;
        int l = 0;
            while (counter <= 40 && rarray[i] != '/0'){
            l = (rand() % 26) +'A';
            rarray[i] = ("%c", l);
            i++;
            counter++;
            if (counter == 40)
                rarray[i] = '/0';
            }
            return rarray;
    }
    void *generated(char string1[])
    {
        char input[40] = {0};
        int i = 0;
        printf("Please enter a string of Uppercase letters.\n");
        string1 = gets(input);
        if (string1 != NULL)
            {
            if (input[i] >= 'A' && input[i] <= 'Z'){
                puts("You entered \t");
                puts(string1);
            }
        else
            puts("Error please enter capital letters.");
    }
    }
    char *symbol(char symb[], char rarray[], char string1[],char Farray[])
    {
        char input_symbol[40];
        char symbol;
        int count;
        int i = 0;
        printf("Please enter a character or symbol.\n");
        symb = gets(input_symbol);
        //puts(symb);
        for (count = 0; count < 40 && Farray[i] != NULL; count++){
            if (string1[i] == rarray[i]){
                Farray[i] = string1[i];
                Farray[i] = puts(symb);
                i++;
            }
            else
            {
                Farray[i] = string1[i];
                i++;
            }
        Farray[40] = '\0';
        }
    }

  2. #2
    Registered User
    Join Date
    Apr 2012
    Posts
    9
    There are numerous issues with your code, but your main problem is the fact that you didn't return a value from symbol() like you said you would. Also, you might find that your Farray array could be a bit wrong. After you fix the return issue, you might want to examine your code on line 69 again. It doesn't necessarily do what you think it does.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    8
    Ahh, yeah I went from a void to a char in that function and forgot the return, thank you. Now if i wouldn't use puts to place the string in that array index then what else can be used? I tried assigning the value without puts and that had the same none working result haha.

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    9
    symb is a char, just like Farray[i] is a char. 1 is an int, just like 2 is an int. In other words, you should just be doing Farray[i] = symb (no functions needed to do that!)

    Note that your value of symb is also wrong in line 64. gets() returns a string (or NULL). How can a whole string fit into a single character when a string is at least two characters (a character, followed by the NUL byte '\0')? What I'm saying is that you're mixing data types. You're trying to do char = char*, which might compile, but it's not right.
    Last edited by kitsune3233; 04-24-2012 at 01:59 AM. Reason: Clarified what two characters make up a string that is actually usable

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    8
    So i changed the function to this, but It still doesnt put out my farray string after filling it with both the original string with symbol replacements it remains blank. I changed symbol to a getchar and tried both %c, symb and plain ol' symb.
    Code:
    char *symbol(char symb, char rarray[], char string1[],char Farray[])
    {
        char input_symbol;
        char symbol;
        int count;
        int i = 0;
        printf("Please enter a character or symbol.\n");
        symb = getchar();
        //puts(symb);
        for (count = 0; count <= 40 && Farray[i] != NULL; count++){
            if (string1[i] == rarray[i]){
                Farray[i] = string1[i];
                Farray[i] = ("%c",symb);
                i++;
            }
            else
            {
                Farray[i] = ("%c",string1[i]);
                i++;
            }
            
        Farray[40] = '\0';
        }
        return Farray;
    }

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    9
    Well, you're getting closer...

    Code:
    Farray[i] = ("%c", symb);
    Farray[i] = ("%c", string1[i]);
    What do those two lines do? Remember they're pretty much identical, so they behave the same way. I'll give you a hint: it's not copying the character you want.

    What you're looking for is
    Code:
    Farray[i] = symb;
    and
    Code:
    Farray[i] = string1[i];
    You seem to think you need I/O functions and formatting operations when you don't. Really. It's as easy as copying <A> to <B> instead of copying <result of (maybe) doing something with A> to <B>. After all, a character is just a number, and how do you say x = 1? That's exactly how you write it!

    You might want to pick up a good book or check out a tutorial online; it seems you're having a considerable amount of difficulty.

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    8
    You pretty much cleared up the difficulties for me on using the assignment operator for the array, but even with my return it still doesn't display the Farray. The book I've been reading is C by discovery fourth edition, it's a good read but the examples on arrays & pointers and strings never touched on this kind of program. Have you read Let us C?

  8. #8
    Registered User
    Join Date
    Apr 2012
    Posts
    8
    So I've tried many different kinds of string functions because I felt like there may have been something wrong with the if comparison but, now i Dont think so. I'm not sure if the array indices are being populated but I do know it won't print no matter what configuration i do.
    Code:
    char *symbol(char symb, char rarray[], char string1[],char Farray[])
    {
    	char input_symbol;
    	char symbol;
    	int count;
    	int i = 0;
    	printf("Please enter a character or symbol.\n");
    	symb = getchar();
    	//puts(symb);
    	for (count = 0; count < 40 && Farray[i] != NULL; count++){
    		if (strcmp(string1, rarray) == 0){
    			Farray[i] = string1[i];
    			i++;
    		
    		}
    		else
    		{
    		Farray[i] = symb;
    			i++;
    		}
    		
    	Farray[i] = '\0';
    	
    	}
    	return Farray;
    	
    }

  9. #9
    Registered User
    Join Date
    Apr 2012
    Posts
    9
    What you seem to be wanting is if a specific character matches, replace that character with the specified symbol. Your last code did that correctly, meaning your if statement was correct. By using strcmp(), you're comparing the entire string every time. If one character in string1 does not match the character in rarray in the same position, strcmp() will return -1 or 1 and will, according to the if statement you used, execute the else section. And that will happen every time. So if string1 and rarray do not contain EXACTLY the same strings, your else section will execute every time.

    Aside from that, look at line 22 in that last bit of code. You're making your string terminate inside the for loop. Farray[0] = '\0', Farray[1] = '\0', Fararay[2] = '\0', etc. Move it outside the for loop, just before the return statement. See where that gets you.

  10. #10
    Registered User
    Join Date
    Apr 2012
    Posts
    8
    Ahh so string compare takes the entire string as a whole rather then go by individual characters. I moved my farray[i] = Null out of the for loop and above the return statement, but i still get the same result. After inputting the symbol the program closes with no error message.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays of strings
    By ceem in forum C Programming
    Replies: 1
    Last Post: 03-03-2011, 01:17 AM
  2. Arrays/strings
    By Nextstopearth in forum C Programming
    Replies: 2
    Last Post: 10-22-2008, 07:31 PM
  3. arrays of strings
    By mbooka in forum C Programming
    Replies: 2
    Last Post: 02-19-2006, 07:55 PM
  4. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  5. Arrays of Strings are fun
    By kippwinger in forum C++ Programming
    Replies: 9
    Last Post: 07-02-2003, 04:12 PM