My program doesn't print a string

时间:2015-10-30 21:36:28

标签: c

I'm making a program to delete extra spaces in c and count how many extra space it deletes. The program counts the extra spaces but it doesn't print the string that doesn't have extra spaces. I'll show you my code:

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

/*
 * 
 */
char delete_spaces(char oracion[100])
{
    int i;
    for (i=0;oracion[i]!='\0';i++){
        if (oracion[i]==' '&&oracion[i+1]==' '){
            oracion[i]=oracion[i+1];
        }  
    }
    return(oracion[100]);
}

int count_spaces(char oracion[100])
{
    int i,number_spaces=0;
    for (i=0;oracion[i]!='\0';i++){
        if (oracion[i]==' '&&oracion[i+1]==' '){
            number_spaces+=1;
        } 
    }
    return(number_spaces);
}

int main(void){
    char frase[100],frase2[100];
    int num_spaces;
    printf("Write here the phrase:");
    gets(frase);
    frase2[100]=delete_spaces(frase);
    num_spaces=count_spaces(frase);
    printf("%s",frase2);
    printf("%d",num_spaces);
    return 0;
}

4 个答案:

答案 0 :(得分:0)

In addition to Barmar's suggestion above, you probably want frase2 to actually be a char* type, and you want to assign it not to the index 100, but the pointer itself:

int main(void) {
    char frase[100];
    char *frase2;
    ...
    frase2 = delete_spaces(frase);
    ...
}

答案 1 :(得分:0)

You can't assign to an array the way you're trying to do it. You should pass frase2 as a second argument to the function. Then it can copy from one array to the other, skipping over duplicate spaces.

int delete_spaces(char oracion[100], char oracion2[100])
{
    int deletions = 0;
    int i;
    int dest = 0;
    for (i=0; oracion[i]!='\0'; i++){
        if (oracion[i]==' '&&oracion[i+1]==' '){
            deletions++;
        } else {
            oracion2[dest++] = oracion[i];
        }
    }
    oracion2[dest] = '\0'; // don't forget to terminate the string
    return deletions;
}

Notice that this does the counting as it's deleting, so you don't need a separate function for this.

Then you would call it from main as:

num_spaces = delete_spaces(frase, frase2);

答案 2 :(得分:0)

Could be this what you need?:

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

int delete_spaces(char *s1,char *s2){

    int found = 0;
    int i = 0, j = 0;

    while(s1[i] != '\0'){
        if (((s1[i] == ' ') && (s1[i+1] == ' ')) || ((s1[i] == '\t') && (s1[i+1] == '\t'))){
            found++;
        } else {
            s2[j++] = s1[i];
        }
        i++;
    }
    s2[j] = '\0';

    printf("s2 = %s\n",s2);
    return found;
}

int main(void){
    char frase[100], frase2[100];
    int num_spaces;

    printf("Write here the phrase:");
    if((fgets(frase,99,stdin)) == NULL){
        printf("Error\n");
    }

    num_spaces=delete_spaces(frase,frase2);
    printf("Number of deleted spaces = %d\n",num_spaces);
    return 0;
}

Output:

I'm going    to sleep now           Goodbye        
s2 = I'm going to sleep now Goodbye

Number of deleted spaces = 13

答案 3 :(得分:0)

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

typedef struct sentence {//wrap by struct
    char oracion[100];
} sentence;

sentence delete_spaces(sentence frase)//make copy
{
    int i, j;
    for (j=i=0; frase.oracion[i] != '\0'; i++){
        if(j && frase.oracion[j-1] == ' ' && frase.oracion[i] == ' '){
            continue;
        }  
        frase.oracion[j++] = frase.oracion[i];
    }
    frase.oracion[j] = '\0';
    return frase;
}

int count_spaces(sentence frase)
{
    int i, j, number_spaces = 0;
    for (j=i=0; frase.oracion[i] != '\0'; i++){
        if(j && frase.oracion[j-1] == ' ' && frase.oracion[i] == ' '){
            number_spaces += 1;
            continue;
        }  
        frase.oracion[j++] = frase.oracion[i];
    }
    return number_spaces;
}

int main(void){
    sentence frase, frase2;
    int num_spaces;

    printf("Write here the phrase:");
    scanf("%99[^\n]%*c", frase.oracion);
    frase2 = delete_spaces(frase);//make copy to frase2
    num_spaces = count_spaces(frase);
    printf("%s\n", frase2.oracion);
    printf("%d\n", num_spaces);

    return 0;
}