Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <stdio.h>
- #include <conio.h>
- #include <string>
- #include <stdlib.h>
- #define MAX_NORMALIZE_STRING_LEN 1024
- int main(){
- // Plus 1 for end of string
- char buf[MAX_NORMALIZE_STRING_LEN + 1];
- // Longer because of adding spaces after ['.', ';', ',']
- char ret[MAX_NORMALIZE_STRING_LEN*3/2 + 1];
- // Clear screen
- system("cls");
- printf("\r\n>>Input String: ");
- fflush(stdin);
- fgets(buf, MAX_NORMALIZE_STRING_LEN, stdin);
- int len = strlen(buf);
- int pos = 0;
- for(int i =0; i < len; i ++){
- // Process all characters exclude [' ', ';' ',', '.']
- if (buf[i] != ' ' && buf[i] != ',' && buf[i] != '.' && buf[i] != ';')
- {
- ret[pos] = buf[i];
- pos += 1;
- }
- else if ((i + 1) < len && pos > 0){
- /*
- Character i is space, add this to ret if:
- + Next character is not in [' ', ';' ',', '.']
- + ret[pos - 1] is not space
- */
- if (buf[i] == ' ')
- {
- if(buf[i + 1] != ' ' && buf[i + 1] != ',' && buf[i + 1] != '.' && buf[i + 1] != ';'){
- if (ret[pos - 1] != ' ')
- {
- ret[pos] = buf[i];
- pos += 1;
- }
- }
- }
- /*
- Character i is in [';' ',', '.'], add this to ret if ret[pos - 1] not in [' ', ';' ',', '.']
- */
- else if (buf[i] == '.' || buf[i] == ';' || buf[i] == ',')
- {
- if (ret[pos - 1] != ',' && ret[pos - 1] != '.' && ret[pos - 1] != ';' && ret[pos - 1] != ' ')
- {
- ret[pos] = buf[i];
- pos += 1;
- ret[pos] = ' ';
- pos += 1;
- }
- }
- }
- }
- ret[pos] = '\0';
- printf("\r\n>> Ouput String: %s", ret);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement