감 ¶
ver.1 ¶
그
~cpp
/* 그*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char strchange(char ch[50], int lenstr);
void main()
{
char ch[50], *pCh;
int lenstr;
FILE *fp1, *fp2;
pCh = ch;
fp1 = fopen("source.txt", "r");
fgets(ch, 50, fp1); //
fclose(fp1);
printf("Before string = %s \n", ch);
lenstr = strlen(ch);
*pCh = strchange(pCh, lenstr);
fp2 = fopen("result.txt", "w");
printf("After string = %s \n", ch);
fputs(ch, fp2);
fclose(fp2);
}
char strchange(char *pCh, int lenstr)
{
int i;
char temp[50];
for(i = 0; i <= lenstr; i++)
{
temp[i] = *(pCh+i);
}
for(i = 0; i <=lenstr; i++)
{
*(pCh+lenstr-i-1) = temp[i];
}
return *pCh;
}
ver.2 ¶
~cpp /* 그 ver.2*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char strchange(char ch[50], int lenstr, int choiceNum);
void main()
{
char ch[50], *pCh;
int lenstr, choiceNum, i = 0;
FILE *fp1, *fp2;
pCh = ch;
fp1 = fopen("source.txt", "r");
fp2 = fopen("result.txt", "w");
fgets(ch, 50, fp1); //
lenstr = strlen(ch);
if('A'<= ch[i] && ch[i] <='z')
{
choiceNum = 0; //
}else
{
choiceNum = 1; // ??
}
*pCh = strchange(pCh, lenstr, choiceNum);
fputs(ch, fp2);
fclose(fp1);
fclose(fp2);
}
char strchange(char *pCh, int lenstr, int choiceNum)
{
int i;
char temp[50];
switch(choiceNum)
{
case 0:
for(i = 0; i <= lenstr; i++)
{
*(temp+i) = *(pCh+i);
}
for(i = 0; i <= lenstr; i++)
{
*(pCh+lenstr-i-1) = *(temp+i);
}
break;
case 1:
for(i = 0; i < lenstr; i++)
{
*(temp+i) = *(pCh+i);
}
for(i = 0; i < lenstr; i+=2)
{
*(pCh+lenstr-i-2) = *(temp + i);
*(pCh+lenstr-i-1) = *(temp+i+1);
}
break;
}
return *pCh;
}
C++ ¶
~cpp
//cpp1.cpp
#include <iostream.h>
#include <string.h>
#include "cpp.h"
void main()
{
Mystring mystr;
mystr.input();
mystr.str_reverse();
mystr.output();
}
~cpp
//cpp2.cpp
#include <iostream.h>
#include "cpp.h"
#include <string.h>
void Mystring::str_reverse()
{
str_len = strlen(ch);
int i = 0;
while(ch[i])// 간 .
{
str_temp[str_len-i-1] = ch[i];
++i;
}
}
void Mystring::input()
{
cin>>ch;
}
void Mystring::output()
{
int i = 0;
while(str_temp[i] > 0)
{
cout<<str_temp[i];
++i;
}
cout<<"\n";
}
~cpp
//cpp.h
class Mystring
{
private :
char ch[50];
int str_len;
char str_temp[50];
public :
void str_reverse();
void input();
void output();
};










