I'm trying to get a program to print out two strings that I input. I can't figure out why its not working!

#include <stdio.h>
#include <stdlib.h>
#define ARRAY_SIZE 30

int main() {

        char s1[ARRAY_SIZE];
        char s2 [ARRAY_SIZE];

        printf("Enter a String:\n");
        scanf("%25s\n", s1);
        printf("Enter string 2:\n");
        scanf("%25s\n", s2);

        printf("String 1: %s\n", s1);
        printf("String 2: %s\n", s2);

        return(EXIT_SUCCESS);

}

It compiles, but then say if I enter as String 1: I do, and String 2: Will not, it will print out:
I
do

It is like nothing is going into the second string!

Any help is appreciated, thanks.

>It is like nothing is going into the second string!
The first scanf is reading until it encounters the space. The second scanf is reading the remainder, until it encounters a space again or a newline, whichever is first.
scanf is not a good player for reading strings. Use fgets()

Got it.

I did:

fgets(s1, ARRAY_SIZE, stdin);

Thanks a lot!

>But doesn't fgets get input from a file?
Yes, stdin can be that file.

>Is there a way to tell scanf to ignore the spaces?
Yes, but you don't want to learn that.

>I want to prompt the user for input. fgets( string_variable, sizeof string_variable, stdin );

If you can help me with this too:

So I'm trying to practice string manipulation and I'm adding on to that program above. I want to delete all characters in s1 that match any character in s2.

here is my logic:


get first char in s2
compare it all in s1 and when the same, delete char
get next char in s2...

im experimenting with for loops and while loops, and im thinking about using strcmp() and maybe strstr().....if anyone can point me in the right direction I would appreciate it.

this is what I have so far:

int main() {

        char s1[ARRAY_SIZE];
        char s2 [ARRAY_SIZE];

        printf("Enter a String:\n");
        fgets(s1,ARRAY_SIZE, stdin);
        printf("Enter string 2:\n");
        fgets(s2, ARRAY_SIZE, stdin);

        for(int i =0; i !=NULL; i++;) {

                char *result = strstr(s1, s2[i])
                while (result != NULL){

                // Here I want to delete the character, I'm not sure how to do that...
                //do i replace the array cell with a null??

        }       }

>// Here I want to delete the character, I'm not sure how to do that...
Deleting a character essentially means overwriting it by shifting every character after it left by one cell:

abcdefg

    efg
abcd

   efg
abcd

abcefg

A loop to do this is relatively simple:

size_t i = 0;

/* Find the matching character */
while ( s[i] != '\0' ) {
  if ( s[i] == key )
    break;

  ++i;
}

/* Shift everything over it */
while ( s[i] != '\0' ) {
  s[i] = s[i + 1];
  ++i;
}

Ok, so I think I'm having some trouble with the break statement. Here is my code so far:

int main() {

        char s1[ARRAY_SIZE];
        char s2 [ARRAY_SIZE];

        printf("Enter a String:\n");
        fgets(s1,ARRAY_SIZE, stdin);
        printf("Enter string 2:\n");
        fgets(s2, ARRAY_SIZE, stdin);


        char key;
        key = s2[0];
        int i = 0;

        if (s2[key] != '\0') {

                while (s1[i] != '\0') {
                        if (s1[i] == key)
                                break;
                        i++;
                }
//test to see if code continues at this point
                printf("just broke from while loop\n");
                while (s1[i] != '\0'){
                        s1[i] = s1[i + 1];
                        i++;
                }
        key++;
        }

        printf("String 1: %s\n", s1);
        printf("String 2: %s\n", s2);

        return(EXIT_SUCCESS);

}

the break statement seems to take it out of all the loops, so it doesn't do any of the other work, and print out the same two strings that i input. any suggestions?

>if (s2[key] != '\0') {
You probably meant that to be a loop.

Now the code compiles, but I get segmentation fault after it asks me for the two strings.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ARRAY_SIZE 256

int main() {

        char s1[ARRAY_SIZE];
        char s2 [ARRAY_SIZE];

        printf("Enter a String:\n");
        fgets(s1,ARRAY_SIZE, stdin);
        printf("Enter string 2:\n");
        fgets(s2, ARRAY_SIZE, stdin);

        char key;
        key = s2[0];
        int i = 0;
        int next = 0;

        while (next < strlen(s2)) {

                while (s1[i] != '\0') {
                        if (s1[i] == key){
                                s1[i] = s1[i+1];
                                i++;
                        }

                i = 0;
                next++;
                key = s2[next];
                }
        }
        printf("String 1: %s\n", s1);
        printf("String 2: %s\n", s2);

        return(EXIT_SUCCESS);
}

run example:

Enter a String:
tumble
Enter string 2:
bum
Segmentation fault

Thanks for all your help, Narue.

while (s1[i] != '\0') { /* s1[i] is always s1[0] */
        if (s1[i] == key){
            s1[i] = s1[i+1];
            i++;  /* it doesn't matter if you increment here */
         }

         i = 0; /* i always get set to zero before going into the while loop again */
         next++; /* next keeps getting incremented in the loop */
         key = s2[next]; /* before long you are setting key from out of bound array */
}

You have created a nice infinite loop with the right to crash.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.