Tuesday, September 11, 2012

Directory Path Parsing

Input: "C:\\a\\b\\c\\..\\.\\..\\g\\."
Output: "C:\\a\\g"

#include<iostream>
#include<string.h>
#include<signal.h>
using namespace std;
int main()
{
void fun();
char A[100]="C:\\a\\b\\c\\..\\.\\..\\g\\.\\..";
//char A[100]="C:\\a\\b";
char B[100]="";
char temp[100]="";
int top=-1;
char C[20][100];
int i=0;
while(i<strlen(A) && A[i]!='\\')
{
B[i]=A[i];
i++;
}
B[i]=0;
while(i<strlen(A))
{
int j=0;
while(i<strlen(A) && A[i]!='\\')
{
temp[j]=A[i];
i++;
j++;
}
temp[j]=0;
if(top>=0 && strcmp(temp,"..")==0)
{
top--;
}
else if(strcmp(temp,".")==0)
{
}
else
{
top++;
strcpy(C[top],temp);
}
i++;
}
for(int j=0;j<=top;j++)
{
strcat(B,C[j]);
strcat(B,"\\");
}
cout<<B<<endl;
}

No comments:

Post a Comment