Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int str1_len(char* str1);
- int str2_len(char* str2);
- int str3_len(char* str3);
- char* str_cpy(char* str1, char* str2); //скопировать str1 в str2 строку;
- char* str_cat(char* str2,char* str); //записать вторую строку в первую
- int str_cmp(char* str2,char* str); // сравнение строк (вычитать символы 1 из другой
- char* str_str(char* str,char* str2); //поиск строки в строке
- int words_count(char* str1); // подсчет колличества слов
- int ch_str(char* str1, char ch); // кол-во символов в строке
- int sum_of_digits(char* str); // сумма цифр
- int sum_of_decimal(char* str); // сумма чисел
- char* reverse_str(char* str); // переворот всего массива
- char* reverse_substr(char* str, int start, int end); // переворот определенного куска массива через swap
- void swap(char* a, char* b);
- int longest_word(char* str);
- int shorter_word(char* str);
- void print_words(char* str);
- char* reverse_words(char* str);
- char* s_gets(char* str, int buf_size);
- char* s_gets_c(char* str, int buf_size);
- char* remove_dup(char* str);
- int main()
- { const int size = 50;
- char str1[size] = "Hello world i am fine!";
- const int SIZE = 50;
- char str2[SIZE] = "It is str2 string";
- const int SiZe = 50;
- char str3[size] = "worldddgkllabcd";
- char ch;
- // s_gets(str1,size);
- //s_gets_c(str1,buf_size);
- //printf("Введите число:");
- //scanf("%s",&ch);
- int start = 0, end = 0;
- //printf("Введите Start:");
- // scanf("%d",&start);
- // printf("Введите end:");
- // scanf("%d",&end);
- //printf("str1_len : %d\n",str1_len(str1));
- //printf("str2_len : %d\n",str2_len(str2));
- //printf("str_cpy: %s\n",str_cpy(str2,str1));
- //printf("str_cat: %s\n",str_cat(str1,str2));
- //printf("str_cmp: %d\n",str_cmp(str1,str2));
- //printf("ch_str:%d\n",ch_str(str1,ch));
- //printf("words_count:%d\n",words_count(str1));
- //printf("sum_of_digits:%d\n",sum_of_digits(str3));
- //printf("sum_of_decimal:%d\n",sum_of_decimal(str3));
- //reverse_str(str1);
- //printf("reverse_substr:%s\n",reverse_substr(str1,start,end));
- //printf("longest_word:%d\n",longest_word(str2));
- //printf("shorter_word:%d\n",shorter_word(str1));
- //printf("str_str:%s\n",str_str(str1,str3));
- //print_words(str1);
- //reverse_words(str1); не рабит(
- printf("remove_dup:%s\n",remove_dup(str3));
- return 0;
- }
- int str1_len(char* str1)
- {
- int i = 0;
- while(str1[i]){
- i++;
- }
- return i;
- }
- int str2_len(char* str2)
- {
- int i = 0;
- while (str2[i]){
- i++;
- }
- return i;
- }
- int str3_len(char* str3)
- {
- int i = 0;
- while(str3[i]){
- i++;
- }
- return i;
- }
- char* str_cpy(char* str2, char* str1)
- {
- for(int j = 0;str2[j];j++){
- str2[j] = 0;
- for(int i = 0; str1[i];i++){
- str2[i] = str1[i];
- }
- }
- return str2;
- }
- char* str_cat(char* str1,char* str2)
- {
- int i = 0;
- int STr1_len = str1_len(str1);
- str1[STr1_len] = ' ';
- while (str1[i]){
- str1[i + STr1_len] = str2[i];
- i++;
- }
- return str1;
- }
- int str_cmp(char* str1,char* str2)
- {
- int i = 0;
- while (str1[i] && str2[i]){
- i++;
- }
- if((str1[i] - str2[i])== 0) return 1; else return 0;
- }
- char* str_str(char* str1,char* str2)
- {
- int i = 0, j = 0, flag = 1;
- while(str1[i]) {
- flag = 1;
- while(str2[j]){
- if(str1[i] != str2[j]){
- flag = 0;
- break;
- }
- j++;
- }
- if(flag) return str2;
- i++;
- }
- return NULL;
- }
- int ch_str(char* str1, char ch)
- {
- int i = 0,n = -1;
- while(str1[i]){
- n++;
- if(str1[i] == ch)
- return n;
- i++;
- }
- return -1;
- }
- int words_count(char* str1)
- {
- int i = 0;
- int counter = 0,word = 0;
- char *dst = ".,-_\n\t!?&' '";
- while(str1[i]) {
- if(ch_str(dst,str1[i]) != -1) {
- if(word) {
- counter++;
- word = 0;
- }
- }
- else {
- word = 1;
- }
- i++;
- }
- if (word) counter++;
- return counter;
- }
- int sum_of_digits(char* str3)
- {
- int sum = 0;
- for(int i = 0; str3[i];i++){
- if(str3[i] >='0' && str3[i]<='9')
- {
- sum +=str3[i] - '0';
- }
- }
- return sum;
- }
- int sum_of_decimal(char* str3)
- {
- int sum = 0, counter = 0, i = 0;
- while (str3[i]){
- while(str3[i]>= '0' && str3[i]<= '9'){
- counter = counter * 10 + str3[i] - '0';
- i++;
- }
- i++;
- sum += counter;
- counter= 0;
- }
- return sum;
- }
- char* reverse_str(char* str1)
- {
- int i = 0;
- while (str1[i]){
- i++;
- }
- for(int j = i - 1; j >= 0; j--){
- printf("%c",str1[j]);
- }
- printf("\n");
- return 0;
- }
- void swap(char* a, char* b)
- {
- char temp = *a;
- *a = *b;
- *b = temp;
- }
- char* reverse_substr(char* str1, int start, int end)
- {
- while (start <= end){
- swap(&str1[start++],&str1[end--]);
- }
- return str1;
- }
- int longest_word(char* str)
- {
- int i = 0,current_task = 0,max_task = 0,word = 0;
- char* dst = ".,-_\n\t!?&5' '";
- while(str[i]){
- if(ch_str(dst,str[i]) != -1) {
- if(word) {
- if (current_task > max_task)
- max_task = current_task;
- current_task = 0;
- word = 0;
- }
- } else {
- word = 1;
- current_task++;
- }
- i++;
- }
- if(word && current_task > max_task)
- max_task = current_task;
- return max_task;
- }
- int shorter_word(char* str)
- {
- int i = 0,current_task = 0,min_task = str1_len(str),word = 0;
- char* dst = ".,-_\n\t!?&5' '";
- while(str[i]){
- if(ch_str(dst,str[i]) != -1) {
- if(word) {
- if (current_task < min_task)
- min_task = current_task;
- current_task = 0;
- word = 0;
- }
- } else {
- word = 1;
- current_task++;
- }
- i++;
- }
- if(word && current_task < min_task)
- min_task = current_task;
- return min_task;
- }
- void print_words(char* str)
- {
- char *dst = ".,-_\n\t!?&' '";
- for(int i = 0;str[i];i++){
- if(ch_str(dst,str[i]) != -1){
- printf("\n");
- } else {
- printf("%c",str[i]);
- }
- }
- printf("\n");
- }
- char* reverse_words (char* str)
- {
- int j = 0, k = str1_len(str) - 1;
- char *dst = ".,-_\n\t!?&' '";
- for(int i = 0;str[i];i++){
- if(ch_str(dst,str[i]) != -1){
- printf("\n");
- } else {
- for(j,k;j < k;j++,k--){
- int temp = str[j];
- str[j] = str[k];
- str[k] = temp;
- }
- printf("%c",str[i]);
- }
- }
- printf("\n");
- return str;
- }
- char* s_gets(char* str, int buf_size){
- int i = 0;
- if(!fgets(str,buf_size,stdin))
- return NULL;
- while(str[i]){
- if(str[i] == '\n'){
- break;
- }
- i++;
- }
- str[i] = '\0';
- while(getchar() != '\n');
- return str;
- }
- char* s_gets_c(char* str, int buf_size){
- int i = 0;
- char ch = '\0';
- while((ch = getchar()) != '\n'){
- if (i < buf_size){
- str[i]= ch;
- }
- }
- str[i] = '\0';
- return str;
- }
- char* remove_dup(char* str)
- {
- int i = 0, count = 0, j = 0, flag = 1;
- while(str[i]){
- j = 0;
- flag = 1;
- while(j < count){
- if (str[i] == str[j]){
- flag = 0;
- break;
- }
- j++;
- }
- if(flag) {
- str[count] =str[i];
- count++;
- }
- i++;
- }
- str[count] ='\0';
- return str;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement