Функция удаления пробелов - C (СИ)
Формулировка задачи:
Помогите с написанием функции для удаления первого пробела в строках, на картинке показано, и удаления пробелов перед знаками припинания. Заранее спасибо.
вот код:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#define SIZE 5
char *getStr();
char ** work(char*);
char *clear_probel(char *);
int main() {
setlocale(LC_ALL, ".1251");
char *str=NULL,**result=NULL;
int i;
while (printf("Введите строку: \n"), str = getStr()){
result=work(str);
for (i=0; result[i]!=NULL; i++){
//clear_probel(result[i]);
//printf("i=%d\n", i);
printf("Строкa: \n%s\n",result[i]);
}
}
// system("pause");
return 0;
}
char *clear_probel(char *str) {
char*newstr, *q;
int i;
unsigned long l;
q=str;
newstr=(char*)malloc(strlen(q)+1);
strcpy(newstr, "");
for (i=0; *(q+=strspn(q, " \t")); i++, q+=l) {
l = strcspn(q, " \t");
if (strlen(newstr))
strcat(newstr, " ");
strncat(newstr, q, l);
}
newstr = (char*)realloc(newstr, strlen(newstr) + 1);
return newstr;
}
char ** work (char *str){
int i,k;
char *res, **result, *tmp, *p;
res=clear_probel(str);
result=(char**)malloc(sizeof(char*));
result[0]=NULL;
for (tmp=res, i=0; (p=strchr(tmp, '.'))!=NULL; i++, tmp=p+1){
result=(char**)realloc(result, sizeof(char*)*(i+1));
result[i]=(char*)malloc(p-tmp+2);
strcpy(result[i], "");
strncat(result[i], tmp, p-tmp+1);
//if ((k=(strchr(result[i],' ')))==1){
//}
result[i+1]=NULL;
}
free(res);
return result;
}
char* getStr() {
char *str = (char *)malloc(1);
char buf[SIZE + 1];
int n, len = 0;
*str = '\0';
do {
n = scanf("%5[^\n]", buf, SIZE + 1);
if (n < 0) {
free(str);
str = NULL;
continue;
}
if (n == 0)
scanf("%*c");
else {
len += strlen(buf);
str = (char *)realloc(str, len + 1);
strcat(str, buf);
}
} while (n > 0);
return str;
}Решение задачи: «Функция удаления пробелов»
textual
Листинг программы
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <ctype.h>
#define SIZE 5
char *getStr();
char ** work(char*);
char *clear_probel(char *);
int main() {
setlocale(LC_ALL, ".1251");
char *str=NULL,**result=NULL;
int i;
while (printf("Введите строку: \n"), str = getStr()){
result=work(str);
for (i=0; result[i]!=NULL; i++){
//clear_probel(result[i]);
//printf("i=%d\n", i);
printf("Строкa: \n%s\n",result[i]);
}
}
// system("pause");
return 0;
}
char *clear_probel(char *str) {
char*newstr, *q;
int i;
unsigned long l;
q=str;
newstr=(char*)malloc(strlen(q)+1);
strcpy(newstr, "");
for (i=0; *(q+=strspn(q, " \t")); i++, q+=l) {
l = strcspn(q, " \t");
if (strlen(newstr))
strcat(newstr, " ");
strncat(newstr, q, l);
}
newstr = (char*)realloc(newstr, strlen(newstr) + 1);
return newstr;
}
char ** work (char *str){
int i;
char *res, **result, *tmp, *p,*k,*j;
res=clear_probel(str);
result=(char**)malloc(sizeof(char*));
result[0]=NULL;
for (tmp=res, i=0; (p=strchr(tmp, '.'))!=NULL; i++, tmp=p+1){
result=(char**)realloc(result, sizeof(char*)*(i+1));
result[i]=(char*)malloc(p-tmp+2);
strcpy(result[i], "");
strncat(result[i], tmp, p-tmp+1);
if ((k=(strchr(result[i],' ')))==result[i]){
*result[i]++;
}
result[i+1]=NULL;
}
free(res);
return result;
}
char* getStr() {
char *str = (char *)malloc(1);
char buf[SIZE + 1];
int n, len = 0;
*str = '\0';
do {
n=scanf("%5[^\n]",buf,SIZE + 1);
if (n < 0) {
free(str);
str = NULL;
continue;
}
if (n == 0)
scanf("%*c");
else {
len += strlen(buf);
str = (char *)realloc(str, len + 1);
strcat(str, buf);
}
} while (n > 0);
return str;
}