c - My program doesn't print a string -
i'm making program delete spaces in c , count how many space deletes. program counts spaces doesn't print string doesn't have spaces. i'll show code:
#include <stdio.h> #include <stdlib.h> /* * */ char delete_spaces(char oracion[100]) { int i; (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; (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 phrase:"); gets(frase); frase2[100]=delete_spaces(frase); num_spaces=count_spaces(frase); printf("%s",frase2); printf("%d",num_spaces); return 0; }
could need?:
#include<stdio.h> #include<stdlib.h> #include<string.h> int delete_spaces(char *s1,char *s2){ int found = 0; int = 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 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 sleep goodbye s2 = i'm going sleep goodbye number of deleted spaces = 13
Comments
Post a Comment