WeixiangYan/CodeTransOcean huggingface dataset:
Task: Strip whitespace from a string_Top and tail

C: #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

char *rtrim(const char *s)
{
  while( isspace(*s) || !isprint(*s) ) ++s;
  return strdup(s);
}

char *ltrim(const char *s)
{
  char *r = strdup(s);
  if (r != NULL)
  {
    char *fr = r + strlen(s) - 1;
    while( (isspace(*fr) || !isprint(*fr) || *fr == 0) && fr >= r) --fr;
    *++fr = 0;
  }
  return r;
}

char *trim(const char *s)
{
  char *r = rtrim(s);
  char *f = ltrim(r);
  free(r);
  return f;
}

const char *a = "     this is a string      ";

int main()
{
  char *b = rtrim(a);
  char *c = ltrim(a);
  char *d = trim(a);

  printf("'%s'\n'%s'\n'%s'\n", b, c, d);
  
  free(b);
  free(c);
  free(d);
  return 0;
}









COBOL: DISPLAY "'" FUNCTION TRIM(str, LEADING) "'"
DISPLAY "'" FUNCTION TRIM(str, TRAILING) "'"
DISPLAY "'" FUNCTION TRIM(str) "'"
Task: Strip whitespace from a string_Top and tail



C#: using System;

public class TrimExample
{
    public static void Main(String[] args)
    {
        const string toTrim = " Trim me ";
        Console.WriteLine(Wrap(toTrim.TrimStart()));
        Console.WriteLine(Wrap(toTrim.TrimEnd()));
        Console.WriteLine(Wrap(toTrim.Trim()));
    }

    private static string Wrap(string s)
    {
        return "'" + s + "'";
    }
}







COBOL: DISPLAY "'" FUNCTION TRIM(str, LEADING) "'"
DISPLAY "'" FUNCTION TRIM(str, TRAILING) "'"
DISPLAY "'" FUNCTION TRIM(str) "'"
Task: Strip whitespace from a string_Top and tail


C++: #include <boost/algorithm/string.hpp>
#include <string>
#include <iostream>

int main( ) {
   std::string testphrase( "    There are unwanted blanks here!    " ) ;
   std::string lefttrimmed = boost::trim_left_copy( testphrase ) ;
   std::string righttrimmed = boost::trim_right_copy( testphrase ) ;
   std::cout << "The test phrase is :" << testphrase << "\n" ;
   std::cout << "Trimmed on the left side :" << lefttrimmed << "\n" ;
   std::cout << "Trimmed on the right side :" << righttrimmed << "\n" ;
   boost::trim( testphrase ) ;
   std::cout << "Trimmed on both sides :" <<  testphrase  << "\n" ;
   return 0 ;
}








COBOL: DISPLAY "'" FUNCTION TRIM(str, LEADING) "'"
DISPLAY "'" FUNCTION TRIM(str, TRAILING) "'"
DISPLAY "'" FUNCTION TRIM(str) "'"
Task: Strip whitespace from a string_Top and tail





Java: public class Trims{
    public static String ltrim(String s) {
        int i = 0;
        while (i < s.length() && Character.isWhitespace(s.charAt(i))) {
            i++;
        }
        return s.substring(i);
    }

    public static String rtrim(String s) {
        int i = s.length() - 1;
        while (i > 0 && Character.isWhitespace(s.charAt(i))) {
            i--;
        }
        return s.substring(0, i + 1);
    }

    public static String trim(String s) {
    	return rtrim(ltrim(s));
    }

    public static void main(String[] args) {
        String s = " \t \r \n String with spaces \u2009 \t  \r  \n  ";
        System.out.printf("[%s]\n", ltrim(s));
        System.out.printf("[%s]\n", rtrim(s));
        System.out.printf("[%s]\n", trim(s));
    }
}





COBOL: DISPLAY "'" FUNCTION TRIM(str, LEADING) "'"
DISPLAY "'" FUNCTION TRIM(str, TRAILING) "'"
DISPLAY "'" FUNCTION TRIM(str) "'"
Task: Strip whitespace from a string_Top and tail







Python: >>> s = ' \t \r \n String with spaces  \t  \r  \n  '
>>> s
' \t \r \n String with spaces  \t  \r  \n  '
>>> s.lstrip()
'String with spaces  \t  \r  \n  '
>>> s.rstrip()
' \t \r \n String with spaces'
>>> s.strip()
'String with spaces'
>>>



COBOL: DISPLAY "'" FUNCTION TRIM(str, LEADING) "'"
DISPLAY "'" FUNCTION TRIM(str, TRAILING) "'"
DISPLAY "'" FUNCTION TRIM(str) "'"
Task: Strip whitespace from a string_Top and tail








VB: Public Sub test()
    
    
    
    s = " trim "
    Debug.Print """" & s & """"
    Debug.Print """" & LTrim(s) & """"
    Debug.Print """" & RTrim(s) & """"
    Debug.Print """" & WorksheetFunction.trim(s) & """"
    
End Sub


COBOL: DISPLAY "'" FUNCTION TRIM(str, LEADING) "'"
DISPLAY "'" FUNCTION TRIM(str, TRAILING) "'"
DISPLAY "'" FUNCTION TRIM(str) "'"
Task: Strip whitespace from a string_Top and tail




Go: package main

import (
    "fmt"
    "strings"
    "unicode"
)

var simple = `
    simple   `

func main() {
    show("original", simple)
    show("leading ws removed", strings.TrimLeftFunc(simple, unicode.IsSpace))
    show("trailing ws removed", strings.TrimRightFunc(simple, unicode.IsSpace))
    
    show("both removed", strings.TrimSpace(simple))
}

func show(label, str string) {
    fmt.Printf("%s: |%s| %v\n", label, str, []rune(str))
}






COBOL: DISPLAY "'" FUNCTION TRIM(str, LEADING) "'"
DISPLAY "'" FUNCTION TRIM(str, TRAILING) "'"
DISPLAY "'" FUNCTION TRIM(str) "'"
Task: Strip whitespace from a string_Top and tail






PHP: <?php 



$string = '      this is a string     ';
echo '^'.trim($string) .'$'.PHP_EOL;
echo '^'.ltrim($string).'$'.PHP_EOL;
echo '^'.rtrim($string).'$'.PHP_EOL;
Task: Strip whitespace from a string_Top and tail






PHP: <?php 



$string = '      this is a string     ';
echo '^'.trim($string) .'$'.PHP_EOL;
echo '^'.ltrim($string).'$'.PHP_EOL;
echo '^'.rtrim($string).'$'.PHP_EOL;






Ada: with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings; use Ada.Strings;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
procedure StripDemo is
   str : String := "     Jabberwocky     ";
begin
   Put_Line ("'" & Trim (str, Left) & "'");
   Put_Line ("'" & Trim (str, Right) & "'");
   Put_Line ("'" & Trim (str, Both) & "'");
end StripDemo;
Task: Strip whitespace from a string_Top and tail






PHP: <?php 



$string = '      this is a string     ';
echo '^'.trim($string) .'$'.PHP_EOL;
echo '^'.ltrim($string).'$'.PHP_EOL;
echo '^'.rtrim($string).'$'.PHP_EOL;




COBOL: DISPLAY "'" FUNCTION TRIM(str, LEADING) "'"
DISPLAY "'" FUNCTION TRIM(str, TRAILING) "'"
DISPLAY "'" FUNCTION TRIM(str) "'"
Task: Strip comments from a string



C#: using System.Text.RegularExpressions;

string RemoveComments(string str, string delimiter)
        {
            
            
            
            return Regex.Replace(str, delimiter + ".+", string.Empty).Trim();
        }









Ada: with Ada.Text_IO;
procedure Program is
   Comment_Characters : String := "#;";
begin
   loop
      declare
	 Line : String := Ada.Text_IO.Get_Line;
      begin
	 exit when Line'Length = 0;
	 Outer_Loop : for I in Line'Range loop
	    for J in Comment_Characters'Range loop
	       if Comment_Characters(J) = Line(I) then
		  Ada.Text_IO.Put_Line(Line(Line'First .. I - 1));
		  exit Outer_Loop;
	       end if;
	    end loop;
	 end loop Outer_Loop;
      end;
   end loop;
end Program;
Task: Strip comments from a string

C: #include<stdio.h>

int main()
{
	char ch, str[100];
	int i;
	
	do{
		printf("\nEnter the string :");
		fgets(str,100,stdin);
		for(i=0;str[i]!=00;i++)
		{
			if(str[i]=='#'||str[i]==';')
			{
				str[i]=00;
				break;
			}
		}
		printf("\nThe modified string is : %s",str);
		printf("\nDo you want to repeat (y/n): ");
		scanf("%c",&ch);
		fflush(stdin);
	}while(ch=='y'||ch=='Y');
	
	return 0;
}











Ada: with Ada.Text_IO;
procedure Program is
   Comment_Characters : String := "#;";
begin
   loop
      declare
	 Line : String := Ada.Text_IO.Get_Line;
      begin
	 exit when Line'Length = 0;
	 Outer_Loop : for I in Line'Range loop
	    for J in Comment_Characters'Range loop
	       if Comment_Characters(J) = Line(I) then
		  Ada.Text_IO.Put_Line(Line(Line'First .. I - 1));
		  exit Outer_Loop;
	       end if;
	    end loop;
	 end loop Outer_Loop;
      end;
   end loop;
end Program;
Task: Strip comments from a string


C++: #include <iostream>
#include <string>

std::string strip_white(const std::string& input)
{
   size_t b = input.find_first_not_of(' ');
   if (b == std::string::npos) b = 0;
   return input.substr(b, input.find_last_not_of(' ') + 1 - b);
}

std::string strip_comments(const std::string& input, const std::string& delimiters)
{
   return strip_white(input.substr(0, input.find_first_of(delimiters)));
}
 
int main( ) {
   std::string input;
   std::string delimiters("#;");
   while ( getline(std::cin, input) && !input.empty() ) {
      std::cout << strip_comments(input, delimiters) << std::endl ;
   }
   return 0;
}










Ada: with Ada.Text_IO;
procedure Program is
   Comment_Characters : String := "#;";
begin
   loop
      declare
	 Line : String := Ada.Text_IO.Get_Line;
      begin
	 exit when Line'Length = 0;
	 Outer_Loop : for I in Line'Range loop
	    for J in Comment_Characters'Range loop
	       if Comment_Characters(J) = Line(I) then
		  Ada.Text_IO.Put_Line(Line(Line'First .. I - 1));
		  exit Outer_Loop;
	       end if;
	    end loop;
	 end loop Outer_Loop;
      end;
   end loop;
end Program;
Task: Strip comments from a string




Go: package main

import (
	"fmt"
	"strings"
	"unicode"
)

const commentChars = "#;"

func stripComment(source string) string {
	if cut := strings.IndexAny(source, commentChars); cut >= 0 {
		return strings.TrimRightFunc(source[:cut], unicode.IsSpace)
	}
	return source
}

func main() {
	for _, s := range []string{
		"apples, pears # and bananas",
		"apples, pears ; and bananas",
		"no bananas",
	} {
		fmt.Printf("source:   %q\n", s)
		fmt.Printf("stripped: %q\n", stripComment(s))
	}
}








Ada: with Ada.Text_IO;
procedure Program is
   Comment_Characters : String := "#;";
begin
   loop
      declare
	 Line : String := Ada.Text_IO.Get_Line;
      begin
	 exit when Line'Length = 0;
	 Outer_Loop : for I in Line'Range loop
	    for J in Comment_Characters'Range loop
	       if Comment_Characters(J) = Line(I) then
		  Ada.Text_IO.Put_Line(Line(Line'First .. I - 1));
		  exit Outer_Loop;
	       end if;
	    end loop;
	 end loop Outer_Loop;
      end;
   end loop;
end Program;
Task: Strip comments from a string





Java: import java.io.*;

public class StripLineComments{
    public static void main( String[] args ){
	if( args.length < 1 ){
	    System.out.println("Usage: java StripLineComments StringToProcess");
	}
	else{
	    String inputFile = args[0];
	    String input = "";
	    try{
		BufferedReader reader = new BufferedReader( new FileReader( inputFile ) );
		String line = "";
		while((line = reader.readLine()) != null){
		    System.out.println( line.split("[#;]")[0] );
		}
	    }
	    catch( Exception e ){
		e.printStackTrace();
	    }
	}
    }
}







Ada: with Ada.Text_IO;
procedure Program is
   Comment_Characters : String := "#;";
begin
   loop
      declare
	 Line : String := Ada.Text_IO.Get_Line;
      begin
	 exit when Line'Length = 0;
	 Outer_Loop : for I in Line'Range loop
	    for J in Comment_Characters'Range loop
	       if Comment_Characters(J) = Line(I) then
		  Ada.Text_IO.Put_Line(Line(Line'First .. I - 1));
		  exit Outer_Loop;
	       end if;
	    end loop;
	 end loop Outer_Loop;
      end;
   end loop;
end Program;
Task: Strip comments from a string







Python: def remove_comments(line, sep):
    for s in sep:
        i = line.find(s)
        if i >= 0:
            line = line[:i]
    return line.strip()


print remove_comments('apples ; pears 
print remove_comments('apples ; pears 





Ada: with Ada.Text_IO;
procedure Program is
   Comment_Characters : String := "#;";
begin
   loop
      declare
	 Line : String := Ada.Text_IO.Get_Line;
      begin
	 exit when Line'Length = 0;
	 Outer_Loop : for I in Line'Range loop
	    for J in Comment_Characters'Range loop
	       if Comment_Characters(J) = Line(I) then
		  Ada.Text_IO.Put_Line(Line(Line'First .. I - 1));
		  exit Outer_Loop;
	       end if;
	    end loop;
	 end loop Outer_Loop;
      end;
   end loop;
end Program;
Task: Strip comments from a string








VB: Function strip_comments(s,char)
	If InStr(1,s,char) > 0 Then
		arr = Split(s,char)
		strip_comments = RTrim(arr(0))
	Else
		strip_comments = s
	End If
End Function

WScript.StdOut.WriteLine strip_comments("apples, pears # and bananas","#")
WScript.StdOut.WriteLine strip_comments("apples, pears ; and bananas",";")




Ada: with Ada.Text_IO;
procedure Program is
   Comment_Characters : String := "#;";
begin
   loop
      declare
	 Line : String := Ada.Text_IO.Get_Line;
      begin
	 exit when Line'Length = 0;
	 Outer_Loop : for I in Line'Range loop
	    for J in Comment_Characters'Range loop
	       if Comment_Characters(J) = Line(I) then
		  Ada.Text_IO.Put_Line(Line(Line'First .. I - 1));
		  exit Outer_Loop;
	       end if;
	    end loop;
	 end loop Outer_Loop;
      end;
   end loop;
end Program;
Task: Strip comments from a string

C: #include<stdio.h>

int main()
{
	char ch, str[100];
	int i;
	
	do{
		printf("\nEnter the string :");
		fgets(str,100,stdin);
		for(i=0;str[i]!=00;i++)
		{
			if(str[i]=='#'||str[i]==';')
			{
				str[i]=00;
				break;
			}
		}
		printf("\nThe modified string is : %s",str);
		printf("\nDo you want to repeat (y/n): ");
		scanf("%c",&ch);
		fflush(stdin);
	}while(ch=='y'||ch=='Y');
	
	return 0;
}
Task: Strip comments from a string



C#: using System.Text.RegularExpressions;

string RemoveComments(string str, string delimiter)
        {
            
            
            
            return Regex.Replace(str, delimiter + ".+", string.Empty).Trim();
        }
Task: Strip comments from a string


C++: #include <iostream>
#include <string>

std::string strip_white(const std::string& input)
{
   size_t b = input.find_first_not_of(' ');
   if (b == std::string::npos) b = 0;
   return input.substr(b, input.find_last_not_of(' ') + 1 - b);
}

std::string strip_comments(const std::string& input, const std::string& delimiters)
{
   return strip_white(input.substr(0, input.find_first_of(delimiters)));
}
 
int main( ) {
   std::string input;
   std::string delimiters("#;");
   while ( getline(std::cin, input) && !input.empty() ) {
      std::cout << strip_comments(input, delimiters) << std::endl ;
   }
   return 0;
}
Task: Strip comments from a string





Java: import java.io.*;

public class StripLineComments{
    public static void main( String[] args ){
	if( args.length < 1 ){
	    System.out.println("Usage: java StripLineComments StringToProcess");
	}
	else{
	    String inputFile = args[0];
	    String input = "";
	    try{
		BufferedReader reader = new BufferedReader( new FileReader( inputFile ) );
		String line = "";
		while((line = reader.readLine()) != null){
		    System.out.println( line.split("[#;]")[0] );
		}
	    }
	    catch( Exception e ){
		e.printStackTrace();
	    }
	}
    }
}
Task: Strip comments from a string







Python: def remove_comments(line, sep):
    for s in sep:
        i = line.find(s)
        if i >= 0:
            line = line[:i]
    return line.strip()


print remove_comments('apples ; pears 
print remove_comments('apples ; pears
Task: Strip comments from a string








VB: Function strip_comments(s,char)
	If InStr(1,s,char) > 0 Then
		arr = Split(s,char)
		strip_comments = RTrim(arr(0))
	Else
		strip_comments = s
	End If
End Function

WScript.StdOut.WriteLine strip_comments("apples, pears # and bananas","#")
WScript.StdOut.WriteLine strip_comments("apples, pears ; and bananas",";")
Task: Strip comments from a string




Go: package main

import (
	"fmt"
	"strings"
	"unicode"
)

const commentChars = "#;"

func stripComment(source string) string {
	if cut := strings.IndexAny(source, commentChars); cut >= 0 {
		return strings.TrimRightFunc(source[:cut], unicode.IsSpace)
	}
	return source
}

func main() {
	for _, s := range []string{
		"apples, pears # and bananas",
		"apples, pears ; and bananas",
		"no bananas",
	} {
		fmt.Printf("source:   %q\n", s)
		fmt.Printf("stripped: %q\n", stripComment(s))
	}
}
Task: Strip comments from a string



C#: using System.Text.RegularExpressions;

string RemoveComments(string str, string delimiter)
        {
            
            
            
            return Regex.Replace(str, delimiter + ".+", string.Empty).Trim();
        }








Fortran: 
 module string_routines

 implicit none
 private
 public :: strip_comments
 contains

 
	 function strip_comments(str,c) result(str2)
	 implicit none
	 character(len=*),intent(in) :: str
	 character(len=1),intent(in) :: c 
	 character(len=len(str)) :: str2
	 
	 integer :: i
	 
	 i = index(str,c)
	 if (i>0) then
		str2 = str(1:i-1)
	 else
		str2 = str
	 end if
	 
	 end function strip_comments
 

 end module string_routines



 program main



 use string_routines, only: strip_comments
 implicit none
 
 write(*,*) strip_comments('apples, pears # and bananas', '#')
 write(*,*) strip_comments('apples, pears ; and bananas', ';')
 

 end program main
Task: Strip comments from a string


C++: #include <iostream>
#include <string>

std::string strip_white(const std::string& input)
{
   size_t b = input.find_first_not_of(' ');
   if (b == std::string::npos) b = 0;
   return input.substr(b, input.find_last_not_of(' ') + 1 - b);
}

std::string strip_comments(const std::string& input, const std::string& delimiters)
{
   return strip_white(input.substr(0, input.find_first_of(delimiters)));
}
 
int main( ) {
   std::string input;
   std::string delimiters("#;");
   while ( getline(std::cin, input) && !input.empty() ) {
      std::cout << strip_comments(input, delimiters) << std::endl ;
   }
   return 0;
}









Fortran: 
 module string_routines

 implicit none
 private
 public :: strip_comments
 contains

 
	 function strip_comments(str,c) result(str2)
	 implicit none
	 character(len=*),intent(in) :: str
	 character(len=1),intent(in) :: c 
	 character(len=len(str)) :: str2
	 
	 integer :: i
	 
	 i = index(str,c)
	 if (i>0) then
		str2 = str(1:i-1)
	 else
		str2 = str
	 end if
	 
	 end function strip_comments
 

 end module string_routines



 program main



 use string_routines, only: strip_comments
 implicit none
 
 write(*,*) strip_comments('apples, pears # and bananas', '#')
 write(*,*) strip_comments('apples, pears ; and bananas', ';')
 

 end program main
Task: Strip comments from a string

C: #include<stdio.h>

int main()
{
	char ch, str[100];
	int i;
	
	do{
		printf("\nEnter the string :");
		fgets(str,100,stdin);
		for(i=0;str[i]!=00;i++)
		{
			if(str[i]=='#'||str[i]==';')
			{
				str[i]=00;
				break;
			}
		}
		printf("\nThe modified string is : %s",str);
		printf("\nDo you want to repeat (y/n): ");
		scanf("%c",&ch);
		fflush(stdin);
	}while(ch=='y'||ch=='Y');
	
	return 0;
}










Fortran: 
 module string_routines

 implicit none
 private
 public :: strip_comments
 contains

 
	 function strip_comments(str,c) result(str2)
	 implicit none
	 character(len=*),intent(in) :: str
	 character(len=1),intent(in) :: c 
	 character(len=len(str)) :: str2
	 
	 integer :: i
	 
	 i = index(str,c)
	 if (i>0) then
		str2 = str(1:i-1)
	 else
		str2 = str
	 end if
	 
	 end function strip_comments
 

 end module string_routines



 program main



 use string_routines, only: strip_comments
 implicit none
 
 write(*,*) strip_comments('apples, pears # and bananas', '#')
 write(*,*) strip_comments('apples, pears ; and bananas', ';')
 

 end program main
Task: Strip comments from a string





Java: import java.io.*;

public class StripLineComments{
    public static void main( String[] args ){
	if( args.length < 1 ){
	    System.out.println("Usage: java StripLineComments StringToProcess");
	}
	else{
	    String inputFile = args[0];
	    String input = "";
	    try{
		BufferedReader reader = new BufferedReader( new FileReader( inputFile ) );
		String line = "";
		while((line = reader.readLine()) != null){
		    System.out.println( line.split("[#;]")[0] );
		}
	    }
	    catch( Exception e ){
		e.printStackTrace();
	    }
	}
    }
}






Fortran: 
 module string_routines

 implicit none
 private
 public :: strip_comments
 contains

 
	 function strip_comments(str,c) result(str2)
	 implicit none
	 character(len=*),intent(in) :: str
	 character(len=1),intent(in) :: c 
	 character(len=len(str)) :: str2
	 
	 integer :: i
	 
	 i = index(str,c)
	 if (i>0) then
		str2 = str(1:i-1)
	 else
		str2 = str
	 end if
	 
	 end function strip_comments
 

 end module string_routines



 program main



 use string_routines, only: strip_comments
 implicit none
 
 write(*,*) strip_comments('apples, pears # and bananas', '#')
 write(*,*) strip_comments('apples, pears ; and bananas', ';')
 

 end program main
Task: Strip comments from a string







Python: def remove_comments(line, sep):
    for s in sep:
        i = line.find(s)
        if i >= 0:
            line = line[:i]
    return line.strip()


print remove_comments('apples ; pears 
print remove_comments('apples ; pears 




Fortran: 
 module string_routines

 implicit none
 private
 public :: strip_comments
 contains

 
	 function strip_comments(str,c) result(str2)
	 implicit none
	 character(len=*),intent(in) :: str
	 character(len=1),intent(in) :: c 
	 character(len=len(str)) :: str2
	 
	 integer :: i
	 
	 i = index(str,c)
	 if (i>0) then
		str2 = str(1:i-1)
	 else
		str2 = str
	 end if
	 
	 end function strip_comments
 

 end module string_routines



 program main



 use string_routines, only: strip_comments
 implicit none
 
 write(*,*) strip_comments('apples, pears # and bananas', '#')
 write(*,*) strip_comments('apples, pears ; and bananas', ';')
 

 end program main
Task: Strip comments from a string








VB: Function strip_comments(s,char)
	If InStr(1,s,char) > 0 Then
		arr = Split(s,char)
		strip_comments = RTrim(arr(0))
	Else
		strip_comments = s
	End If
End Function

WScript.StdOut.WriteLine strip_comments("apples, pears # and bananas","#")
WScript.StdOut.WriteLine strip_comments("apples, pears ; and bananas",";")



Fortran: 
 module string_routines

 implicit none
 private
 public :: strip_comments
 contains

 
	 function strip_comments(str,c) result(str2)
	 implicit none
	 character(len=*),intent(in) :: str
	 character(len=1),intent(in) :: c 
	 character(len=len(str)) :: str2
	 
	 integer :: i
	 
	 i = index(str,c)
	 if (i>0) then
		str2 = str(1:i-1)
	 else
		str2 = str
	 end if
	 
	 end function strip_comments
 

 end module string_routines



 program main



 use string_routines, only: strip_comments
 implicit none
 
 write(*,*) strip_comments('apples, pears # and bananas', '#')
 write(*,*) strip_comments('apples, pears ; and bananas', ';')
 

 end program main
Task: Strip comments from a string

C: #include<stdio.h>

int main()
{
	char ch, str[100];
	int i;
	
	do{
		printf("\nEnter the string :");
		fgets(str,100,stdin);
		for(i=0;str[i]!=00;i++)
		{
			if(str[i]=='#'||str[i]==';')
			{
				str[i]=00;
				break;
			}
		}
		printf("\nThe modified string is : %s",str);
		printf("\nDo you want to repeat (y/n): ");
		scanf("%c",&ch);
		fflush(stdin);
	}while(ch=='y'||ch=='Y');
	
	return 0;
}









COBOL:        identification division.
       program-id. StripComments.

       data division.
       working-storage section.
       01  line-text              pic x(64).

       procedure division.
       main.
           move "apples, pears # and bananas" to line-text
           perform show-striped-text

           move "apples, pears ; and bananas" to line-text
           perform show-striped-text

           stop run
           .
       show-striped-text.
           unstring line-text delimited by "#" or ";" into line-text
           display quote, function trim(line-text), quote
           .
Task: Strip comments from a string



C#: using System.Text.RegularExpressions;

string RemoveComments(string str, string delimiter)
        {
            
            
            
            return Regex.Replace(str, delimiter + ".+", string.Empty).Trim();
        }







COBOL:        identification division.
       program-id. StripComments.

       data division.
       working-storage section.
       01  line-text              pic x(64).

       procedure division.
       main.
           move "apples, pears # and bananas" to line-text
           perform show-striped-text

           move "apples, pears ; and bananas" to line-text
           perform show-striped-text

           stop run
           .
       show-striped-text.
           unstring line-text delimited by "#" or ";" into line-text
           display quote, function trim(line-text), quote
           .